From 5004f54b969629500c66e8e86506c53927dbc7c4 Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Fri, 1 Dec 2023 01:18:57 -0600 Subject: [PATCH] Fix or individually ignore the errors in the PHPstan baseline. --- phpstan-baseline.neon | 70 ------------------- .../Migration/Traits/UpdateAllRecords.php | 69 ++++++++++++++++++ .../Migration/Version20180412055024.php | 16 ++--- .../Migration/Version20180429013130.php | 7 +- .../Migration/Version20180818223558.php | 7 +- .../Migration/Version20190513163051.php | 7 +- .../Migration/Version20201204043539.php | 6 +- src/Normalizer/DoctrineEntityNormalizer.php | 5 +- .../Constraints/UniqueEntityValidator.php | 7 +- src/Xml/Reader.php | 7 ++ 10 files changed, 113 insertions(+), 88 deletions(-) create mode 100644 src/Entity/Migration/Traits/UpdateAllRecords.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6149a473f..364905f71 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,72 +1,2 @@ parameters: ignoreErrors: - - - message: "#^Parameter \\#3 \\$criteria of method Doctrine\\\\DBAL\\\\Connection\\:\\:update\\(\\) expects array\\, array\\ given\\.$#" - count: 1 - path: src/Entity/Migration/Version20180412055024.php - - - - message: "#^Parameter \\#3 \\$criteria of method Doctrine\\\\DBAL\\\\Connection\\:\\:update\\(\\) expects array\\, array\\ given\\.$#" - count: 1 - path: src/Entity/Migration/Version20180429013130.php - - - - message: "#^Parameter \\#3 \\$criteria of method Doctrine\\\\DBAL\\\\Connection\\:\\:update\\(\\) expects array\\, array\\ given\\.$#" - count: 1 - path: src/Entity/Migration/Version20180818223558.php - - - - message: "#^Parameter \\#3 \\$criteria of method Doctrine\\\\DBAL\\\\Connection\\:\\:update\\(\\) expects array\\, array\\ given\\.$#" - count: 1 - path: src/Entity/Migration/Version20190513163051.php - - - - message: "#^Parameter \\#2 \\$criteria of method Doctrine\\\\DBAL\\\\Connection\\:\\:delete\\(\\) expects array\\, array\\ given\\.$#" - count: 1 - path: src/Entity/Migration/Version20201204043539.php - - - - message: "#^Method App\\\\Normalizer\\\\DoctrineEntityNormalizer\\:\\:getAllowedAttributes\\(\\) should return array\\|false but returns array\\\\|bool\\.$#" - count: 1 - path: src/Normalizer/DoctrineEntityNormalizer.php - - - - message: "#^Parameter \\#1 \\$className of method Doctrine\\\\ORM\\\\EntityManagerInterface\\:\\:getRepository\\(\\) expects class\\-string\\, class\\-string\\|false given\\.$#" - count: 1 - path: src/Validator/Constraints/UniqueEntityValidator.php - - - - message: "#^Access to an undefined property XMLReader\\:\\:\\$depth\\.$#" - count: 1 - path: src/Xml/Reader.php - - - - message: "#^Access to an undefined property XMLReader\\:\\:\\$hasAttributes\\.$#" - count: 1 - path: src/Xml/Reader.php - - - - message: "#^Access to an undefined property XMLReader\\:\\:\\$isEmptyElement\\.$#" - count: 1 - path: src/Xml/Reader.php - - - - message: "#^Access to an undefined property XMLReader\\:\\:\\$localName\\.$#" - count: 1 - path: src/Xml/Reader.php - - - - message: "#^Access to an undefined property XMLReader\\:\\:\\$name\\.$#" - count: 1 - path: src/Xml/Reader.php - - - - message: "#^Access to an undefined property XMLReader\\:\\:\\$nodeType\\.$#" - count: 1 - path: src/Xml/Reader.php - - - - message: "#^Access to an undefined property XMLReader\\:\\:\\$value\\.$#" - count: 2 - path: src/Xml/Reader.php - diff --git a/src/Entity/Migration/Traits/UpdateAllRecords.php b/src/Entity/Migration/Traits/UpdateAllRecords.php new file mode 100644 index 000000000..348f87034 --- /dev/null +++ b/src/Entity/Migration/Traits/UpdateAllRecords.php @@ -0,0 +1,69 @@ + $data Column-value pairs + * @param array|array $types Parameter types + * + * @return int|string The number of affected rows. + * + * @throws Exception + */ + public function updateAllRecords( + string $table, + array $data, + array $types = [] + ): int|string { + $columns = $values = $set = []; + + foreach ($data as $columnName => $value) { + $columns[] = $columnName; + $values[] = $value; + $set[] = $columnName . ' = ?'; + } + + if (is_string(key($types))) { + $types = $this->extractTypeValues($columns, $types); + } + + $sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $set); + return $this->connection->executeStatement($sql, $values, $types); + } + + /** + * Extract ordered type list from an ordered column list and type map. + * + * @param array $columnList + * @param array|array $types + * + * @return array|array + */ + private function extractTypeValues(array $columnList, array $types): array + { + $typeValues = []; + + foreach ($columnList as $columnName) { + $typeValues[] = $types[$columnName] ?? ParameterType::STRING; + } + + return $typeValues; + } +} diff --git a/src/Entity/Migration/Version20180412055024.php b/src/Entity/Migration/Version20180412055024.php index e7ddd045d..315ea5e37 100644 --- a/src/Entity/Migration/Version20180412055024.php +++ b/src/Entity/Migration/Version20180412055024.php @@ -4,14 +4,14 @@ declare(strict_types=1); namespace App\Entity\Migration; +use App\Entity\Migration\Traits\UpdateAllRecords; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; -/** - * Auto-generated Migration: Please modify to your needs! - */ final class Version20180412055024 extends AbstractMigration { + use UpdateAllRecords; + public function up(Schema $schema): void { $this->addSql('ALTER TABLE station_playlists ADD source VARCHAR(50) NOT NULL, ADD include_in_requests TINYINT(1) NOT NULL'); @@ -19,13 +19,13 @@ final class Version20180412055024 extends AbstractMigration public function postup(Schema $schema): void { - $this->connection->update('station_playlists', [ - 'include_in_requests' => 1, - ], ['type' => 'default']); + $this->updateAllRecords('station_playlists', [ + 'include_in_requests' => '1', + ]); - $this->connection->update('station_playlists', [ + $this->updateAllRecords('station_playlists', [ 'source' => 'default', - ], [1 => 1]); + ]); } public function down(Schema $schema): void diff --git a/src/Entity/Migration/Version20180429013130.php b/src/Entity/Migration/Version20180429013130.php index de05e218f..6ceba1ce8 100644 --- a/src/Entity/Migration/Version20180429013130.php +++ b/src/Entity/Migration/Version20180429013130.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Entity\Migration; +use App\Entity\Migration\Traits\UpdateAllRecords; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; @@ -12,6 +13,8 @@ use Doctrine\Migrations\AbstractMigration; */ final class Version20180429013130 extends AbstractMigration { + use UpdateAllRecords; + public function up(Schema $schema): void { $this->addSql('ALTER TABLE station_playlists ADD playback_order VARCHAR(50) NOT NULL, ADD remote_url VARCHAR(255) DEFAULT NULL'); @@ -19,10 +22,10 @@ final class Version20180429013130 extends AbstractMigration public function postup(Schema $schema): void { - $this->connection->update('station_playlists', [ + $this->updateAllRecords('station_playlists', [ 'source' => 'songs', 'playback_order' => 'random', - ], [1 => 1]); + ]); } public function down(Schema $schema): void diff --git a/src/Entity/Migration/Version20180818223558.php b/src/Entity/Migration/Version20180818223558.php index 35d8b02ef..5449f47a5 100644 --- a/src/Entity/Migration/Version20180818223558.php +++ b/src/Entity/Migration/Version20180818223558.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Entity\Migration; +use App\Entity\Migration\Traits\UpdateAllRecords; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; @@ -12,6 +13,8 @@ use Doctrine\Migrations\AbstractMigration; */ final class Version20180818223558 extends AbstractMigration { + use UpdateAllRecords; + public function up(Schema $schema): void { $this->addSql('ALTER TABLE station ADD api_history_items SMALLINT NOT NULL'); @@ -19,9 +22,9 @@ final class Version20180818223558 extends AbstractMigration public function postup(Schema $schema): void { - $this->connection->update('station', [ + $this->updateAllRecords('station', [ 'api_history_items' => 5, - ], [1 => 1]); + ]); } public function down(Schema $schema): void diff --git a/src/Entity/Migration/Version20190513163051.php b/src/Entity/Migration/Version20190513163051.php index 00d5f52a2..0c84614be 100644 --- a/src/Entity/Migration/Version20190513163051.php +++ b/src/Entity/Migration/Version20190513163051.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Entity\Migration; +use App\Entity\Migration\Traits\UpdateAllRecords; use DateTime; use DateTimeZone; use Doctrine\DBAL\Schema\Schema; @@ -14,6 +15,8 @@ use Doctrine\Migrations\AbstractMigration; */ final class Version20190513163051 extends AbstractMigration { + use UpdateAllRecords; + public function up(Schema $schema): void { // Move "play once per day" playlists to be standard scheduled ones with the same start/end time. @@ -36,9 +39,9 @@ final class Version20190513163051 extends AbstractMigration } // Set all stations' timezones to this value. - $this->connection->update('station', [ + $this->updateAllRecords('station', [ 'timezone' => $globalTz, - ], [1 => 1]); + ]); // Calculate the offset of any currently scheduled playlists. if ('UTC' !== $globalTz) { diff --git a/src/Entity/Migration/Version20201204043539.php b/src/Entity/Migration/Version20201204043539.php index faec8b0f7..ab7593941 100644 --- a/src/Entity/Migration/Version20201204043539.php +++ b/src/Entity/Migration/Version20201204043539.php @@ -74,7 +74,11 @@ final class Version20201204043539 extends AbstractMigration } ); - $this->connection->delete('settings', [1 => 1]); + $this->connection->executeStatement( + <<<'SQL' + DELETE FROM settings + SQL + ); foreach ($newSettings as $settingKey => $settingValue) { $this->connection->insert('settings', [ diff --git a/src/Normalizer/DoctrineEntityNormalizer.php b/src/Normalizer/DoctrineEntityNormalizer.php index db12bd531..2b1aeec1f 100644 --- a/src/Normalizer/DoctrineEntityNormalizer.php +++ b/src/Normalizer/DoctrineEntityNormalizer.php @@ -14,6 +14,7 @@ use InvalidArgumentException; use ReflectionClass; use ReflectionException; use ReflectionProperty; +use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; @@ -130,13 +131,13 @@ final class DoctrineEntityNormalizer extends AbstractObjectNormalizer * @param object|class-string $classOrObject * @param array $context * @param bool $attributesAsString - * @return array|false + * @return string[]|AttributeMetadataInterface[]|bool */ protected function getAllowedAttributes( $classOrObject, array $context, bool $attributesAsString = false - ): array|false { + ): array|bool { $groups = $this->getGroups($context); if (empty($groups)) { return false; diff --git a/src/Validator/Constraints/UniqueEntityValidator.php b/src/Validator/Constraints/UniqueEntityValidator.php index ec647f4ed..13eaf449c 100644 --- a/src/Validator/Constraints/UniqueEntityValidator.php +++ b/src/Validator/Constraints/UniqueEntityValidator.php @@ -19,6 +19,7 @@ use DateTimeInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Iterator; use IteratorAggregate; +use RuntimeException; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -124,7 +125,11 @@ final class UniqueEntityValidator extends ConstraintValidator ); } } else { - $repository = $this->em->getRepository(get_class($value)); + $repoClass = get_class($value); + if (!$repoClass) { + throw new RuntimeException('Invalid class specified.'); + } + $repository = $this->em->getRepository($repoClass); } $result = $repository->{$constraint->repositoryMethod}($criteria); diff --git a/src/Xml/Reader.php b/src/Xml/Reader.php index 48884da0f..b8eabf6d1 100644 --- a/src/Xml/Reader.php +++ b/src/Xml/Reader.php @@ -64,14 +64,18 @@ final class Reader $text = ''; while ($reader->read()) { + // @phpstan-ignore-next-line if ($reader->nodeType === XMLReader::ELEMENT) { + // @phpstan-ignore-next-line if ($reader->depth === 0) { return self::processNextElement($reader); } $attributes = self::getAttributes($reader); + // @phpstan-ignore-next-line $name = $reader->name; + // @phpstan-ignore-next-line if ($reader->isEmptyElement) { $child = []; } else { @@ -102,6 +106,7 @@ final class Reader } elseif ($reader->nodeType === XMLReader::END_ELEMENT) { break; } elseif (in_array($reader->nodeType, self::$textNodes)) { + // @phpstan-ignore-next-line $text .= $reader->value; } } @@ -118,8 +123,10 @@ final class Reader { $attributes = []; + // @phpstan-ignore-next-line if ($reader->hasAttributes) { while ($reader->moveToNextAttribute()) { + // @phpstan-ignore-next-line $attributes['@' . $reader->localName] = $reader->value; }