From cac2c63cf9faa850518b8110babf5c71a5359994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Zrn=C3=ADk?= Date: Sun, 12 Feb 2023 13:10:43 +0100 Subject: [PATCH] update phpstan version --- composer.json | 4 +- composer.lock | 47 ++++++++--------- examples/Accounts/Updater/AccountFactory.php | 3 ++ phpstan.neon | 2 - src/Column.php | 20 +++---- src/Queries/Makers/IQueryMaker.php | 9 ++-- src/Queries/Makers/QueryMakerMySQL.php | 52 ++++++++++++------- src/Queries/Makers/QueryMakerSQLite.php | 24 +++++---- src/Queries/Tables/ColumnDescription.php | 4 +- src/Repository/BaseRepository.php | 1 + src/Repository/Fetch/FetchQuery.php | 4 +- src/Repository/Fetch/ResultService.php | 2 +- src/Repository/Saver/Saver.php | 9 ++-- src/Tracy/Measure.php | 11 ++-- src/Tracy/Panel.php | 50 ++++++++++-------- src/Updater.php | 12 ++--- .../EntityReflection/FetchArrayData.php | 12 ++++- src/Utilities/Reflection.php | 8 +-- src/Utils.php | 28 +++++----- .../Mock/MockSQLMaker_ExistingTable_First.php | 5 +- .../MockSQLMaker_ExistingTable_Second.php | 5 +- .../MockSQLMaker_NotExistingTable_First.php | 5 +- .../MockSQLMaker_NotExistingTable_Second.php | 5 +- .../Mock/Repository/HookExampleConverter.php | 5 +- 24 files changed, 186 insertions(+), 141 deletions(-) diff --git a/composer.json b/composer.json index 4b95fc7..19c6690 100644 --- a/composer.json +++ b/composer.json @@ -41,8 +41,8 @@ "require-dev": { "tracy/tracy": "^2", "phpunit/phpunit": "^9", - "phpstan/phpstan": "^0.12", - "phpstan/phpstan-phpunit": "^0.12", + "phpstan/phpstan": "^1.8", + "phpstan/phpstan-phpunit": "^1.3.4", "nette/neon": "^3", "brick/date-time": "^0.3", "zrnik/phpunit-exceptions": "^0.0.5" diff --git a/composer.lock b/composer.lock index 2932fc5..56df393 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "50fc0cbca98297108982330808b4cb2c", + "content-hash": "5999e7293bd73c82e8daa015447e7fad", "packages": [ { "name": "jetbrains/phpstorm-attributes", @@ -603,20 +603,20 @@ }, { "name": "phpstan/phpstan", - "version": "0.12.100", + "version": "1.9.17", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "48236ddf823547081b2b153d1cd2994b784328c3" + "reference": "204e459e7822f2c586463029f5ecec31bb45a1f2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/48236ddf823547081b2b153d1cd2994b784328c3", - "reference": "48236ddf823547081b2b153d1cd2994b784328c3", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/204e459e7822f2c586463029f5ecec31bb45a1f2", + "reference": "204e459e7822f2c586463029f5ecec31bb45a1f2", "shasum": "" }, "require": { - "php": "^7.1|^8.0" + "php": "^7.2|^8.0" }, "conflict": { "phpstan/phpstan-shim": "*" @@ -626,11 +626,6 @@ "phpstan.phar" ], "type": "library", - "extra": { - "branch-alias": { - "dev-master": "0.12-dev" - } - }, "autoload": { "files": [ "bootstrap.php" @@ -641,9 +636,13 @@ "MIT" ], "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], "support": { "issues": "https://github.com/phpstan/phpstan/issues", - "source": "https://github.com/phpstan/phpstan/tree/0.12.100" + "source": "https://github.com/phpstan/phpstan/tree/1.9.17" }, "funding": [ { @@ -659,39 +658,37 @@ "type": "tidelift" } ], - "time": "2022-11-01T09:52:08+00:00" + "time": "2023-02-08T12:25:00+00:00" }, { "name": "phpstan/phpstan-phpunit", - "version": "0.12.22", + "version": "1.3.4", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan-phpunit.git", - "reference": "7c01ef93bf128b4ac8bdad38c54b2a4fd6b0b3cc" + "reference": "d77af96c1aaec28f7c0293677132eaaad079e01b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/7c01ef93bf128b4ac8bdad38c54b2a4fd6b0b3cc", - "reference": "7c01ef93bf128b4ac8bdad38c54b2a4fd6b0b3cc", + "url": "https://api.github.com/repos/phpstan/phpstan-phpunit/zipball/d77af96c1aaec28f7c0293677132eaaad079e01b", + "reference": "d77af96c1aaec28f7c0293677132eaaad079e01b", "shasum": "" }, "require": { - "php": "^7.1 || ^8.0", - "phpstan/phpstan": "^0.12.92" + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.3" }, "conflict": { "phpunit/phpunit": "<7.0" }, "require-dev": { + "nikic/php-parser": "^4.13.0", "php-parallel-lint/php-parallel-lint": "^1.2", - "phpstan/phpstan-strict-rules": "^0.12.6", + "phpstan/phpstan-strict-rules": "^1.0", "phpunit/phpunit": "^9.5" }, "type": "phpstan-extension", "extra": { - "branch-alias": { - "dev-master": "0.12-dev" - }, "phpstan": { "includes": [ "extension.neon", @@ -711,9 +708,9 @@ "description": "PHPUnit extensions and rules for PHPStan", "support": { "issues": "https://github.com/phpstan/phpstan-phpunit/issues", - "source": "https://github.com/phpstan/phpstan-phpunit/tree/0.12.22" + "source": "https://github.com/phpstan/phpstan-phpunit/tree/1.3.4" }, - "time": "2021-08-12T10:53:43+00:00" + "time": "2023-02-09T08:05:29+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/examples/Accounts/Updater/AccountFactory.php b/examples/Accounts/Updater/AccountFactory.php index 87063fd..d0de53b 100644 --- a/examples/Accounts/Updater/AccountFactory.php +++ b/examples/Accounts/Updater/AccountFactory.php @@ -44,10 +44,13 @@ public function getAccountById(int $id): ?Account { $statement = $this->pdo->prepare('SELECT * FROM account WHERE id = :id'); $statement->execute(['id' => $id]); + $result = $statement->fetch(PDO::FETCH_ASSOC); + if ($result === false) { return null; } + return Account::fromArray(iterator_to_array($result)); } diff --git a/phpstan.neon b/phpstan.neon index bc123ee..11deade 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,8 +3,6 @@ parameters: # checkExplicitmixed: true paths: - src - - tests - - examples includes: - vendor/phpstan/phpstan-phpunit/extension.neon diff --git a/src/Column.php b/src/Column.php index c1783e1..b95b3c4 100644 --- a/src/Column.php +++ b/src/Column.php @@ -163,9 +163,9 @@ public function getNotNull(): bool //region Default Value /** - * @var mixed|null + * @var float|bool|int|string|null */ - private mixed $default = null; + private float|bool|int|string|null $default = null; /** * Allowed types of default values. @@ -180,11 +180,11 @@ public function getNotNull(): bool /** * Set or unset (with null) default value of column. - * @param mixed|null $defaultValue + * @param float|bool|int|string|null $defaultValue * @return $this * @throws InvalidArgumentException */ - public function setDefault(mixed $defaultValue = null): Column + public function setDefault(float|bool|int|string $defaultValue = null): Column { $type = gettype($defaultValue); @@ -209,9 +209,9 @@ public function setDefault(mixed $defaultValue = null): Column /** * Gets a default value. * - * @return mixed + * @return scalar|null */ - public function getDefault(): mixed + public function getDefault(): float|bool|int|string|null { return $this->default ?? null; } @@ -219,15 +219,15 @@ public function getDefault(): mixed //region Column Comment /** - * @var string|null + * @var float|bool|int|string|null */ - private ?string $comment = null; + private float|bool|int|string|null $comment = null; /** * Returns string that was set as a comment. - * @return string|null + * @return float|bool|int|string|null */ - public function getComment(): ?string + public function getComment(): float|bool|int|string|null { return $this->comment; } diff --git a/src/Queries/Makers/IQueryMaker.php b/src/Queries/Makers/IQueryMaker.php index 78db8ae..0bd2a05 100644 --- a/src/Queries/Makers/IQueryMaker.php +++ b/src/Queries/Makers/IQueryMaker.php @@ -120,11 +120,14 @@ public static function removeForeignKey(Table $table, Column $column, string $Fo public static function compareType(string $type1, string $type2): bool; /** - * @param string|null $comment1 - * @param string|null $comment2 + * @param float|bool|int|string|null $comment1 + * @param float|bool|int|string|null $comment2 * @return bool */ - public static function compareComment(?string $comment1, ?string $comment2): bool; + public static function compareComment( + float|bool|int|string|null $comment1, + float|bool|int|string|null $comment2 + ): bool; //endregion } diff --git a/src/Queries/Makers/QueryMakerMySQL.php b/src/Queries/Makers/QueryMakerMySQL.php index 3fc99c1..4efab5f 100644 --- a/src/Queries/Makers/QueryMakerMySQL.php +++ b/src/Queries/Makers/QueryMakerMySQL.php @@ -45,6 +45,10 @@ public static function describeTable(PDO $pdo, Table $table): ?TableDescription $QueryInfo->isExecuted = true; $Statement->execute(); + /** + * @var string $result + * @phpstan-ignore-next-line => there is 'Create Table', trust me :) + */ $result = $Statement->fetch(PDO::FETCH_ASSOC)['Create Table']; $QueryInfo->isSuccess = true; @@ -325,7 +329,7 @@ public static function createUniqueIndexQuery(Table $table, Column $column, ?Tab * @param string $uniqueIndex * @param TableDescription|null $oldTableDescription * @param ColumnDescription|null $columnDescription - * @return array|null + * @return array|null * @throws InvalidArgumentException */ public static function removeUniqueIndexQuery(Table $table, Column $column, string $uniqueIndex, ?TableDescription $oldTableDescription, ?ColumnDescription $columnDescription): ?array @@ -333,14 +337,16 @@ public static function removeUniqueIndexQuery(Table $table, Column $column, stri //Remove Foreign Keys and then add them back after the index was removed! $Queries = []; - foreach ($columnDescription?->foreignKeys as $foreignKeyName) { - $DropQueries = static::removeForeignKey($table, $column, $foreignKeyName, $oldTableDescription, $columnDescription); - foreach ($DropQueries as $DropQuery) { - $DropQuery->setReason("Invoked by 'removeUniqueIndexQuery[" . $uniqueIndex . "]'" . PHP_EOL . $DropQuery->getReason()); - } + if($columnDescription !== null) { + foreach ($columnDescription->foreignKeys as $foreignKeyName) { + $DropQueries = static::removeForeignKey($table, $column, $foreignKeyName, $oldTableDescription, $columnDescription); + foreach ($DropQueries as $DropQuery) { + $DropQuery->setReason("Invoked by 'removeUniqueIndexQuery[" . $uniqueIndex . "]'" . PHP_EOL . $DropQuery->getReason()); + } - foreach ($DropQueries as $dropQuery) { - $Queries[] = $dropQuery; + foreach ($DropQueries as $dropQuery) { + $Queries[] = $dropQuery; + } } } @@ -349,17 +355,19 @@ public static function removeUniqueIndexQuery(Table $table, Column $column, stri ->setReason("There is unexpected unique index '" . $uniqueIndex . "' on '" . $table->getName() . '.' . $column->getName() . "'."); - foreach ($columnDescription?->foreignKeys as $foreignKeyTarget => $foreignKeyName) { - $CreateQueries = static::createForeignKey($table, $column, $foreignKeyTarget, $oldTableDescription, $columnDescription); + if($columnDescription !== null) { + foreach ($columnDescription->foreignKeys as $foreignKeyTarget => $foreignKeyName) { + $CreateQueries = static::createForeignKey($table, $column, $foreignKeyTarget, $oldTableDescription, $columnDescription); - foreach ($CreateQueries as $CreateQuery) { - $CreateQuery->setReason("Invoked by 'removeUniqueIndexQuery[" . $uniqueIndex . "]'" . PHP_EOL . $CreateQuery->getReason()); - } + foreach ($CreateQueries as $CreateQuery) { + $CreateQuery->setReason("Invoked by 'removeUniqueIndexQuery[" . $uniqueIndex . "]'" . PHP_EOL . $CreateQuery->getReason()); + } - foreach ($CreateQueries as $query) { - $Queries[] = $query; - } + foreach ($CreateQueries as $query) { + $Queries[] = $query; + } + } } return $Queries; @@ -435,12 +443,16 @@ public static function compareType(string $type1, string $type2): bool } /** - * @param string|null $comment1 - * @param string|null $comment2 + * @param float|bool|int|string|null $comment1 + * @param float|bool|int|string|null $comment2 * @return bool */ - public static function compareComment(?string $comment1, ?string $comment2): bool + public static function compareComment( + float|bool|int|string|null $comment1, + float|bool|int|string|null $comment2 + ): bool { - return $comment1 === $comment2; + // '==' intended + return $comment1 == $comment2; } } diff --git a/src/Queries/Makers/QueryMakerSQLite.php b/src/Queries/Makers/QueryMakerSQLite.php index 869ef30..9e70d47 100644 --- a/src/Queries/Makers/QueryMakerSQLite.php +++ b/src/Queries/Makers/QueryMakerSQLite.php @@ -66,6 +66,7 @@ public static function describeTable(PDO $pdo, Table $table): ?TableDescription $QueryInfo->isExecuted = true; $Statement->execute(); + /** @var array>|false $SQLiteTableData */ $SQLiteTableData = $Statement->fetchAll(PDO::FETCH_ASSOC); $QueryInfo->isSuccess = true; @@ -94,7 +95,7 @@ public static function describeTable(PDO $pdo, Table $table): ?TableDescription $sql = str_replace( ["\r", "\n", 'CREATE TABLE "' . $table->getName() . '"'], [' ', ' ', 'CREATE TABLE ' . $table->getName()], - $PartRow['sql'] + (string)$PartRow['sql'] ); while (Strings::contains($sql, ' ')) { @@ -321,10 +322,12 @@ public static function alterTableColumnQuery( $keyInBothArrays = [$table->getPrimaryKeyName()]; foreach ($MoveColumns as $columnName) { - foreach ($oldTableDescription?->columns as $subColumnDescription) { - if ($subColumnDescription->column->getName() === $columnName) { - $keyInBothArrays[] = $columnName; - break; + if($oldTableDescription !== null) { + foreach ($oldTableDescription->columns as $subColumnDescription) { + if ($subColumnDescription->column->getName() === $columnName) { + $keyInBothArrays[] = $columnName; + break; + } } } } @@ -581,13 +584,16 @@ public static function compareType(string $type1, string $type2): bool } /** - * @param string|null $comment1 - * @param string|null $comment2 + * @param float|bool|int|string|null $comment1 + * @param float|bool|int|string|null $comment2 * @return bool */ - public static function compareComment(?string $comment1, ?string $comment2): bool + public static function compareComment( + float|bool|int|string|null $comment1, + float|bool|int|string|null $comment2, + ): bool { - //Comments not supported by SQLite, just report that its correct anyhow + //Comments not supported by SQLite, just report that its correct return true; } diff --git a/src/Queries/Tables/ColumnDescription.php b/src/Queries/Tables/ColumnDescription.php index b89d307..34a43f8 100644 --- a/src/Queries/Tables/ColumnDescription.php +++ b/src/Queries/Tables/ColumnDescription.php @@ -35,9 +35,9 @@ class ColumnDescription public bool $notNull = false; /** - * @var string|null + * @var float|bool|int|string|null */ - public ?string $comment = null; + public float|bool|int|string|null $comment = null; /** * @var string|null diff --git a/src/Repository/BaseRepository.php b/src/Repository/BaseRepository.php index b3f1009..0cf3901 100644 --- a/src/Repository/BaseRepository.php +++ b/src/Repository/BaseRepository.php @@ -153,6 +153,7 @@ public function distinctValues(string $className, string $propertyName): array return []; // Dafuq? Guess we should handle this somehow... } + /** @var array>|false $data */ $data = $pdoStatement->fetchAll(PDO::FETCH_ASSOC); if ($data === false) { diff --git a/src/Repository/Fetch/FetchQuery.php b/src/Repository/Fetch/FetchQuery.php index 2ac4b02..4a13250 100644 --- a/src/Repository/Fetch/FetchQuery.php +++ b/src/Repository/Fetch/FetchQuery.php @@ -25,7 +25,7 @@ final private function __construct( /** * @param PDO $pdo - * @return mixed[] + * @return array> */ public function fetchAll(PDO $pdo): array { @@ -46,7 +46,7 @@ public function fetchAll(PDO $pdo): array * @see https://www.php.net/manual/en/pdostatement.fetchall.php#refsect1-pdostatement.fetchall-changelog * @noinspection PhpUnnecessaryLocalVariableInspection * - * @var mixed[] $fetch + * @var array> $fetch */ $fetch = $statement->fetchAll(); $this->executedQueries++; diff --git a/src/Repository/Fetch/ResultService.php b/src/Repository/Fetch/ResultService.php index d859586..73028f2 100644 --- a/src/Repository/Fetch/ResultService.php +++ b/src/Repository/Fetch/ResultService.php @@ -81,7 +81,7 @@ public function complete(): bool /** * @param class-string $baseEntityClassString - * @param array $rows + * @param array> $rows */ public function addRows(string $baseEntityClassString, array $rows): void { diff --git a/src/Repository/Saver/Saver.php b/src/Repository/Saver/Saver.php index 58ff36d..a042649 100644 --- a/src/Repository/Saver/Saver.php +++ b/src/Repository/Saver/Saver.php @@ -48,7 +48,6 @@ public function saveEntities(array $entities): void $orderedInserts = $this->sortInsertTableEntities($insertEntities); - $chunkByTables = self::chunkByTables($orderedInserts); foreach ($chunkByTables as $insertChunk) { @@ -147,7 +146,8 @@ private function update(BaseEntity $entity): void $anyChange = false; foreach ($saveArray as $key => $value) { - if (!array_key_exists($key, $originalData) || (string) $value !== (string) $originalData[$key]) { + // != intended + if (!array_key_exists($key, $originalData) || $value != $originalData[$key]) { $anyChange = true; break; } @@ -263,7 +263,10 @@ function insert(array $entities): void $lastPk = (int)$this->pdo->lastInsertId(); //region Fix for MySQL/MariaDB - /** @see https://www.php.net/manual/en/pdo.lastinsertid.php#122009 */ + /** + * @see https://www.php.net/manual/en/pdo.lastinsertid.php#122009 + * @var string $driver + */ $driver = $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME); if (strtolower($driver) === 'mysql') { $lastPk += count($entities) - 1; diff --git a/src/Tracy/Measure.php b/src/Tracy/Measure.php index c1908d1..6221d2a 100644 --- a/src/Tracy/Measure.php +++ b/src/Tracy/Measure.php @@ -203,12 +203,12 @@ public static function queryCountModification(): float //region Structure /** - * @var array> + * @var array */ private static array $_Tables = []; /** - * @var array> + * @var array> */ private static array $_Columns = []; @@ -223,7 +223,7 @@ public static function structureTableList(): array /** * @param string $tableName - * @return array> + * @return array */ public static function structureColumnList(string $tableName): array { @@ -256,14 +256,9 @@ public static function reportStructureColumn(Table $table, Column $column): void self::$_Columns[$table->getName()] = []; } - if (!isset(self::$_Columns[$table->getName()][$column->getName()])) { - self::$_Columns[$table->getName()][$column->getName()] = []; - } - self::$_Columns[$table->getName()][$column->getName()] = $column; } - #[Pure] public static function structureTableCount(): int { return count(self::$_Tables); diff --git a/src/Tracy/Panel.php b/src/Tracy/Panel.php index 3ef1740..0cfa346 100644 --- a/src/Tracy/Panel.php +++ b/src/Tracy/Panel.php @@ -38,7 +38,7 @@ class Panel implements IBarPanel /** * @param int $style - * @return array + * @return array{0: string, 1: string} * @throws Exception */ private function getIconStyle(int $style): array @@ -85,6 +85,7 @@ private function getIconStyle(int $style): array private function loadSvg(string $svgName): string { if (isset(self::$_svgCache[$svgName])) { + /** @var string */ return self::$_svgCache[$svgName]; } @@ -317,7 +318,9 @@ private function subPanelStructure(): Html ) ); - + /** + * @var array{calls: int, objects: Table[]} $_tableDefinition + */ foreach (Measure::structureTableList() as $_tableDefinition) { /** * @var Table $tableObject @@ -332,16 +335,14 @@ private function subPanelStructure(): Html Html::el('td') ->setHtml( //Security: Replace the tag for already escaped html element! - $this->confirmString( - str_replace( - '_', - '​_', - Html::el() - ->setText( - $tableObject->getName() - ) - ->render() - ) + str_replace( + '_', + '​_', + Html::el() + ->setText( + $tableObject->getName() + ) + ->render() ) ) ) @@ -542,7 +543,12 @@ private function subPanelStructure(): Html ->addHtml( Html::el('td') ->addHtml( - (($columnObject->getDefault() === null) ? 'Unset' : Html::el('b')->addText('"')->addText($columnObject->getDefault())->addText('"')) + (($columnObject->getDefault() === null) ? + 'Unset' : + Html::el('b')->addText('"') + ->addText( + (string) $columnObject->getDefault() + )->addText('"')) ) ) @@ -556,7 +562,9 @@ private function subPanelStructure(): Html ->addHtml( Html::el('td') ->addHtml( - (($columnObject->getComment() === null) ? 'Unset' : Html::el('b')->addText('"')->addText($columnObject->getComment())->addText('"')) + (($columnObject->getComment() === null) ? + 'Unset' : Html::el('b')->addText('"') + ->addText((string)$columnObject->getComment())->addText('"')) ) ) @@ -570,7 +578,12 @@ private function subPanelStructure(): Html ->addHtml( Html::el('td') ->addHtml( - (count($columnObject->getForeignKeys()) === 0 ? 'None' : Debugger::dump($columnObject->getForeignKeys(), true)) + (count($columnObject->getForeignKeys()) === 0 + ? 'None' + // @phpstan-ignore-next-line - yes, I can cast it to string... + : (string) Debugger::dump( + $columnObject->getForeignKeys(), true) + ) ) ) @@ -1132,11 +1145,4 @@ private static function yesNo(bool $yesNo, bool $colors = true): Html $element->setText($text); return $element; } - - private function confirmString(mixed $possibleString): string - { - return (string)$possibleString; - } - - } diff --git a/src/Updater.php b/src/Updater.php index 0940a3c..8e6cb7b 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -170,13 +170,6 @@ public function install(): void $timer_total = microtime(true); //region DriverCheck - /** - * We need a correct driver and - * IQueryMaker class for it! - */ - if ($this->getDriverType() === null) { - throw new InvalidDriverException("Driver type is 'NULL'!"); // @codeCoverageIgnore - } $driverName = DriverType::getName($this->getDriverType()); @@ -304,10 +297,11 @@ public function clear(): void } /** - * @return mixed + * @return int */ - public function getDriverType(): mixed + public function getDriverType(): int { + /** @var int $driver */ $driver = DriverType::getValue( $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME), false diff --git a/src/Utilities/EntityReflection/FetchArrayData.php b/src/Utilities/EntityReflection/FetchArrayData.php index 6df5912..fb52661 100644 --- a/src/Utilities/EntityReflection/FetchArrayData.php +++ b/src/Utilities/EntityReflection/FetchArrayData.php @@ -5,6 +5,7 @@ use JetBrains\PhpStorm\Pure; use ReflectionAttribute; use ReflectionProperty; +use Zrnik\MkSQL\Exceptions\MissingAttributeArgumentException; use Zrnik\MkSQL\Repository\Attributes\FetchArray; use Zrnik\MkSQL\Repository\BaseEntity; use Zrnik\MkSQL\Utilities\Reflection; @@ -33,6 +34,15 @@ public function getPropertyName(): string */ public function getTargetClassName(): string { - return Reflection::attributeGetArgument($this->reflectionAttribute); + $targetClassName = Reflection::attributeGetArgument($this->reflectionAttribute); + + if ($targetClassName === null) { + throw new MissingAttributeArgumentException( + $this->reflectionAttribute, 0 + ); + } + + /** @var class-string */ + return $targetClassName; } } \ No newline at end of file diff --git a/src/Utilities/Reflection.php b/src/Utilities/Reflection.php index 428c8f5..53ee5c9 100644 --- a/src/Utilities/Reflection.php +++ b/src/Utilities/Reflection.php @@ -66,12 +66,12 @@ public static function classGetAttribute(ReflectionClass $reflection, string $at /** * @param ReflectionAttribute $reflectionAttribute * @param int $index - * @return mixed - * @noinspection UnknownInspectionInspection - * @noinspection PhpUnused + * @return float|bool|int|string|null */ - public static function attributeGetArgument(ReflectionAttribute $reflectionAttribute, int $index = 0): mixed + #[Pure] + public static function attributeGetArgument(ReflectionAttribute $reflectionAttribute, int $index = 0): float|bool|int|string|null { + /** @var array $args */ $args = $reflectionAttribute->getArguments(); return $args[$index] ?? null; } diff --git a/src/Utils.php b/src/Utils.php index 94710f0..21b7b37 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -159,35 +159,37 @@ public static function confirmForeignKeyTarget(string $keyTarget): string * * Can be NULL! * - * @param string|null $name - * @return string|null + * @param float|bool|int|string|null $name + * @return float|bool|int|string|null * @throws InvalidArgumentException */ - public static function confirmComment(?string $name): ?string + public static function confirmComment(float|bool|int|string|null $name): float|bool|int|string|null { if ($name === null) { return null; } - return self::checkForbiddenWords(self::confirmName($name, [',', '.', ' '])); + return self::checkForbiddenWords(self::confirmName((string)$name, [',', '.', ' '])); } /** * Fall through checking for banned words in string. * Case Insensitive * - * @param string $text - * @return string + * @param float|bool|int|string|null $text + * @return float|bool|int|string|null * @throws InvalidArgumentException */ - public static function checkForbiddenWords(string $text): string + public static function checkForbiddenWords(float|bool|int|string|null $text): float|bool|int|string|null { - foreach (self::$_Forbidden as $ForbiddenWord) { - if (Strings::contains( - strtolower($text), - strtolower($ForbiddenWord) - )) { - throw new InvalidArgumentException("Forbidden word '" . strtolower($ForbiddenWord) . "' encountered!"); + if ($text !== null) { + foreach (self::$_Forbidden as $ForbiddenWord) { + if (Strings::contains( + strtolower((string)$text), + strtolower($ForbiddenWord) + )) { + throw new InvalidArgumentException("Forbidden word '" . strtolower($ForbiddenWord) . "' encountered!"); + } } } diff --git a/tests/Mock/MockSQLMaker_ExistingTable_First.php b/tests/Mock/MockSQLMaker_ExistingTable_First.php index 54f02c2..608aa80 100644 --- a/tests/Mock/MockSQLMaker_ExistingTable_First.php +++ b/tests/Mock/MockSQLMaker_ExistingTable_First.php @@ -141,7 +141,10 @@ public static function compareType(string $type1, string $type2): bool /** * @inheritDoc */ - public static function compareComment(?string $comment1, ?string $comment2): bool + public static function compareComment( + float|bool|int|string|null $comment1, + float|bool|int|string|null $comment2 + ): bool { return true; } diff --git a/tests/Mock/MockSQLMaker_ExistingTable_Second.php b/tests/Mock/MockSQLMaker_ExistingTable_Second.php index 2cf5603..b51fd44 100644 --- a/tests/Mock/MockSQLMaker_ExistingTable_Second.php +++ b/tests/Mock/MockSQLMaker_ExistingTable_Second.php @@ -143,7 +143,10 @@ public static function compareType(string $type1, string $type2): bool /** * @inheritDoc */ - public static function compareComment(?string $comment1, ?string $comment2): bool + public static function compareComment( + float|bool|int|string|null $comment1, + float|bool|int|string|null $comment2 + ): bool { return true; } diff --git a/tests/Mock/MockSQLMaker_NotExistingTable_First.php b/tests/Mock/MockSQLMaker_NotExistingTable_First.php index e2849d8..265ed35 100644 --- a/tests/Mock/MockSQLMaker_NotExistingTable_First.php +++ b/tests/Mock/MockSQLMaker_NotExistingTable_First.php @@ -127,7 +127,10 @@ public static function compareType(string $type1, string $type2): bool /** * @inheritDoc */ - public static function compareComment(?string $comment1, ?string $comment2): bool + public static function compareComment( + float|bool|int|string|null $comment1, + float|bool|int|string|null $comment2 + ): bool { return true; } diff --git a/tests/Mock/MockSQLMaker_NotExistingTable_Second.php b/tests/Mock/MockSQLMaker_NotExistingTable_Second.php index afd2f92..9f8c288 100644 --- a/tests/Mock/MockSQLMaker_NotExistingTable_Second.php +++ b/tests/Mock/MockSQLMaker_NotExistingTable_Second.php @@ -125,7 +125,10 @@ public static function compareType(string $type1, string $type2): bool /** * @inheritDoc */ - public static function compareComment(?string $comment1, ?string $comment2): bool + public static function compareComment( + float|bool|int|string|null $comment1, + float|bool|int|string|null $comment2 + ): bool { return true; } diff --git a/tests/Mock/Repository/HookExampleConverter.php b/tests/Mock/Repository/HookExampleConverter.php index 23ddbf0..ff12f5f 100644 --- a/tests/Mock/Repository/HookExampleConverter.php +++ b/tests/Mock/Repository/HookExampleConverter.php @@ -19,7 +19,6 @@ public function serialize(mixed $value): string|bool throw new ConfigurationNotFoundException('Configuration not found!'); } - if($encoded === false) { throw new Exception('Should not be false!'); } @@ -29,6 +28,10 @@ public function serialize(mixed $value): string|bool public function deserialize(mixed $value): mixed { + if(!is_string($value)) { + throw new ConfigurationNotFoundException('Must be string!'); + } + /** @noinspection JsonEncodingApiUsageInspection */ $decoded = json_decode($value, true);