Skip to content

Commit

Permalink
PST-1908 update dependencies and PHP, add phpstan baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiří Novák committed Jul 30, 2024
1 parent a8b9356 commit 29a6f41
Show file tree
Hide file tree
Showing 21 changed files with 407 additions and 214 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:7.4-cli
FROM php:8.1-cli

ARG COMPOSER_FLAGS="--prefer-dist --no-interaction"
ARG DEBIAN_FRONTEND=noninteractive
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
}
],
"require": {
"php": ">=7.4",
"php": "^8.1",
"ext-json": "*",
"keboola/php-csvtable": "^1.1.1",
"keboola/php-csvtable": "~2.1.0",
"keboola/php-temp": "^2.0",
"keboola/php-utils": "^4.1",
"monolog/monolog": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"phpstan/phpstan": "^0.12.14",
"phpstan/phpstan": "^1.0",
"php-parallel-lint/php-parallel-lint": "^1.2",
"keboola/coding-standard": ">=9.0"
},
Expand Down Expand Up @@ -56,6 +56,9 @@
},
"config": {
"sort-packages": true,
"optimize-autoloader": true
"optimize-autoloader": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
8 changes: 7 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?xml version="1.0"?>
<ruleset name="Project">
<rule ref="vendor/keboola/coding-standard/src/ruleset.xml"/>
</ruleset>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<exclude-pattern>*/</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
<exclude-pattern>*/</exclude-pattern>
</rule>
</ruleset>
151 changes: 151 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
parameters:
ignoreErrors:
-
message: "#^Binary operation \"\\*\" between int\\|string and 0\\.25 results in an error\\.$#"
count: 1
path: src/Cache.php

-
message: "#^Method Keboola\\\\Json\\\\Cache\\:\\:getNext\\(\\) should return array\\|null but returns mixed\\.$#"
count: 2
path: src/Cache.php

-
message: "#^Property Keboola\\\\Json\\\\Cache\\:\\:\\$temp \\(resource\\) in empty\\(\\) is not falsy\\.$#"
count: 3
path: src/Cache.php

-
message: "#^PHPDoc tag @var above a method has no effect\\.$#"
count: 1
path: src/CsvRow.php

-
message: "#^Part \\$value \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/CsvRow.php

-
message: "#^Method Keboola\\\\Json\\\\NodePath\\:\\:getLast\\(\\) should return string but returns string\\|false\\.$#"
count: 1
path: src/NodePath.php

-
message: "#^Cannot access offset string on mixed\\.$#"
count: 1
path: src/Parser.php

-
message: "#^Parameter \\#1 \\$column of method Keboola\\\\Json\\\\CsvRow\\:\\:setValue\\(\\) expects string, mixed given\\.$#"
count: 4
path: src/Parser.php

-
message: "#^Parameter \\#1 \\$data of method Keboola\\\\Json\\\\Parser\\:\\:parse\\(\\) expects array, mixed given\\.$#"
count: 1
path: src/Parser.php

-
message: "#^Parameter \\#1 \\$path of class Keboola\\\\Json\\\\NodePath constructor expects array\\<string\\>, array\\<int, mixed\\> given\\.$#"
count: 1
path: src/Parser.php

-
message: "#^Parameter \\#3 \\$parentId of method Keboola\\\\Json\\\\Parser\\:\\:parse\\(\\) expects array\\|string\\|null, mixed given\\.$#"
count: 1
path: src/Parser.php

-
message: "#^Right side of \\|\\| is always false\\.$#"
count: 1
path: src/Parser.php

-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/Structure.php

-
message: "#^Cannot access offset string on mixed\\.$#"
count: 2
path: src/Structure.php

-
message: "#^Cannot assign offset string to string\\.$#"
count: 1
path: src/Structure.php

-
message: "#^Offset string on string in isset\\(\\) does not exist\\.$#"
count: 2
path: src/Structure.php

-
message: "#^PHPDoc tag @var above a method has no effect\\.$#"
count: 1
path: src/Structure.php

-
message: "#^Parameter \\#1 \\$definitions of method Keboola\\\\Json\\\\Structure\\:\\:validateDefinitions\\(\\) expects array, mixed given\\.$#"
count: 1
path: src/Structure.php

-
message: "#^Parameter \\#1 \\$nodeName of method Keboola\\\\Json\\\\Structure\\:\\:decodeNodeName\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Structure.php

-
message: "#^Parameter \\#2 \\$data of method Keboola\\\\Json\\\\Structure\\:\\:storeValue\\(\\) expects array, mixed given\\.$#"
count: 1
path: src/Structure.php

-
message: "#^Parameter \\#3 \\$newType of method Keboola\\\\Json\\\\Structure\\:\\:handleUpgrade\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Structure.php

-
message: "#^Part \\$value \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/Structure.php

-
message: "#^Cannot access property \\$data on mixed\\.$#"
count: 1
path: tests/phpunit/ParserTest.php

-
message: "#^Parameter \\#1 \\$data of method Keboola\\\\Json\\\\Parser\\:\\:process\\(\\) expects array, mixed given\\.$#"
count: 2
path: tests/phpunit/ParserTest.php

-
message: "#^Cannot access offset 0 on mixed\\.$#"
count: 1
path: tests/phpunit/RealDataTest.php

-
message: "#^Cannot access property \\$components on mixed\\.$#"
count: 1
path: tests/phpunit/RealDataTest.php

-
message: "#^Cannot access property \\$statuses on mixed\\.$#"
count: 1
path: tests/phpunit/RealDataTest.php

-
message: "#^Parameter \\#1 \\$array of function array_walk expects array\\|object, array\\<int, string\\>\\|false given\\.$#"
count: 2
path: tests/phpunit/RealDataTest.php

-
message: "#^Parameter \\#1 \\$data of method Keboola\\\\Json\\\\Parser\\:\\:process\\(\\) expects array, mixed given\\.$#"
count: 5
path: tests/phpunit/RealDataTest.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 4
path: tests/phpunit/RealDataTest.php
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
includes:
- phpstan-baseline.neon

parameters:
checkMissingIterableValueType: false
ignoreErrors:
- '#json_decode expects string, string\|false given#'
- '#Offset .* does not exist on array\|null#'
- '#Cannot access offset 1 on array<int, string>\|false#'
- '#Parameter .* of (function|method) .* expects array, array.*\|.* given#'
- '#Parameter .* of function fgetcsv expects resource, resource\|false given.#'
15 changes: 9 additions & 6 deletions src/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
LoggerInterface $logger,
Structure $structure,
bool $nestedArraysAsJson = false,
bool $strict = false
bool $strict = false,
) {
$this->nestedArrayAsJson = $nestedArraysAsJson;
$this->strict = $strict;
Expand All @@ -39,6 +39,9 @@ public function getLogger(): LoggerInterface
return $this->log;
}

/**
* @param mixed[] $data
*/
public function analyzeData(array $data, string $rootType): void
{
if (empty($data)) {
Expand All @@ -49,10 +52,7 @@ public function analyzeData(array $data, string $rootType): void
$this->structure->saveNodeValue($path, 'nodeType', 'array');
}

/**
* @param mixed $item
*/
private function analyzeItem($item, NodePath $nodePath): string
private function analyzeItem(mixed $item, NodePath $nodePath): string
{
if (is_scalar($item)) {
if ($this->strict) {
Expand Down Expand Up @@ -85,6 +85,9 @@ private function analyzeItem($item, NodePath $nodePath): string
return $nodeType;
}

/**
* @param mixed[] $array
*/
private function analyzeArray(array $array, NodePath $nodePath): void
{
$oldType = null;
Expand Down Expand Up @@ -114,7 +117,7 @@ private function checkType(?string $oldType, string $newType, NodePath $nodePath
{
if (!is_null($oldType) && ($newType !== $oldType) && ($newType !== 'null') && ($oldType !== 'null')) {
throw new JsonParserException(
"Data in '$nodePath' contains incompatible data types '$oldType' and '$newType'."
"Data in '$nodePath' contains incompatible data types '$oldType' and '$newType'.",
);
}
return $newType;
Expand Down
11 changes: 10 additions & 1 deletion src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Keboola\Json;

use function Keboola\Utils\returnBytes;

class Cache
{
/** @var mixed[] */
protected array $data = [];

/**
Expand All @@ -18,12 +21,15 @@ class Cache

protected ?int $memoryLimit = null;

/**
* @param mixed[] $data
*/
public function store(array $data): void
{
// TODO ensure at least X MB is left free (X should be possible to change -> Parser::getCache()->setMemLimit(X))
// either to stop using memory once X mem is used or once X is left from PHP limit
if (ini_get('memory_limit') !== '-1'
&& memory_get_usage() > (\Keboola\Utils\returnBytes(ini_get('memory_limit')) * 0.25)
&& memory_get_usage() > (returnBytes(ini_get('memory_limit')) * 0.25)
|| ($this->memoryLimit !== null && memory_get_usage() > $this->memoryLimit)
) {
// cache
Expand All @@ -41,6 +47,9 @@ public function store(array $data): void
}
}

/**
* @return mixed[]|null
*/
public function getNext(): ?array
{
if (!empty($this->temp) && !feof($this->temp)) {
Expand Down
16 changes: 12 additions & 4 deletions src/CsvRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@

class CsvRow
{
/**
* @var mixed[]
*/
protected array $data = [];

/**
* @var mixed[] $columns
*/
public function __construct(array $columns)
{
$this->data = array_fill_keys($columns, null);
}

/**
* @param mixed $value
* @throws JsonParserException
*/
public function setValue(string $column, $value): void
public function setValue(string $column, mixed $value): void
{
if (!array_key_exists($column, $this->data)) {
throw new JsonParserException(
"Error assigning '{$value}' to a non-existing column '{$column}'!",
[
'columns' => array_keys($this->data),
]
],
);
}

Expand All @@ -36,13 +41,16 @@ public function setValue(string $column, $value): void
[
'type' => gettype($value),
'value' => json_encode($value),
]
],
);
}

$this->data[$column] = $value;
}

/**
* @return mixed[]
*/
public function getRow(): array
{
return $this->data;
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/InconsistentValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Keboola\Json\Exception;

class InconsistentValueException extends \Exception
use Exception;

class InconsistentValueException extends Exception
{
}
6 changes: 5 additions & 1 deletion src/Exception/JsonParserException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
namespace Keboola\Json\Exception;

use Keboola\Utils\Exception;
use Throwable;

class JsonParserException extends Exception
{
public function __construct(string $message = '', array $data = [], int $code = 0, ?\Throwable $previous = null)
/**
* @param array<mixed> $data
*/
public function __construct(string $message = '', array $data = [], int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->setData($data);
Expand Down
3 changes: 2 additions & 1 deletion src/NodePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

class NodePath
{
/** @var array<string> */
private array $path;

/**
* NodePath constructor.
* @param array<string> $path
*/
public function __construct(array $path)
{
Expand Down
Loading

0 comments on commit 29a6f41

Please sign in to comment.