diff --git a/composer.json b/composer.json index b1517bf..7abb916 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,11 @@ ], "require": { "php": ">=8.1", - "phpstan/phpstan": "^1.10.12" + "phpstan/phpstan": "^1.10.12 || ^2.0.0" }, "require-dev": { "phpstan/extension-installer": "^1.1", - "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-deprecation-rules": "^1.0 || ^2.0", "nextras/orm": "~5.0@dev", "nette/tester": "^2.3.1" }, diff --git a/src/Rules/SetValueMethodRule.php b/src/Rules/SetValueMethodRule.php index 706967f..2fef216 100644 --- a/src/Rules/SetValueMethodRule.php +++ b/src/Rules/SetValueMethodRule.php @@ -9,6 +9,7 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\VerbosityLevel; @@ -35,8 +36,6 @@ public function getNodeType(): string /** * @param MethodCall $node - * - * @return string[] */ public function processNode(Node $node, Scope $scope): array { @@ -81,22 +80,22 @@ public function processNode(Node $node, Scope $scope): array } if (!$class->hasProperty($fieldName)) { - $errors[] = sprintf( - 'Entity %s has no $%s property.', - $className, - $fieldName - ); + $errors[] = RuleErrorBuilder::message(sprintf('Entity %s has no $%s property.', $className, $fieldName)) + ->identifier("nextrasOrm.propertyNotFound") + ->build(); continue; } $property = $class->getProperty($fieldName, $scope); if (!$property->isWritable() && $methodName !== 'setReadOnlyValue') { - $errors[] = sprintf( + $errors[] = RuleErrorBuilder::message(sprintf( 'Entity %s: property $%s is read-only.', $className, $fieldName - ); + )) + ->identifier("nextrasOrm.propertyReadOnly") + ->build(); continue; } @@ -106,13 +105,15 @@ public function processNode(Node $node, Scope $scope): array } if (!$propertyType->accepts($valueType, true)->yes()) { - $errors[] = sprintf( + $errors[] = RuleErrorBuilder::message(sprintf( 'Entity %s: property $%s (%s) does not accept %s.', $className, $fieldName, $propertyType->describe(VerbosityLevel::typeOnly()), $valueType->describe(VerbosityLevel::typeOnly()) - ); + )) + ->identifier("nextrasOrm.propertyUnresolvableType") + ->build(); } } diff --git a/tests/testbox/Types/RepositoryTypesTest.php b/tests/testbox/Types/RepositoryTypesTest.php index cb47b20..5088d1b 100644 --- a/tests/testbox/Types/RepositoryTypesTest.php +++ b/tests/testbox/Types/RepositoryTypesTest.php @@ -17,7 +17,10 @@ public function testError(AuthorsRepository $repository, BooksRepository $booksR } - public function testOk(AuthorsRepository $repository): void + /** + * @param AuthorsRepository|BooksRepository $repository2 + */ + public function testOk(AuthorsRepository $repository, $repository2): void { $this->takeAuthor($repository->getByIdChecked(1)); $this->takeAuthor($repository->getByChecked(['id' => 1])); @@ -31,9 +34,7 @@ public function testOk(AuthorsRepository $repository): void $a = $repository->getByIdChecked(1); $this->takeAuthor($repository->persist($a)); - /** @var AuthorsRepository|BooksRepository $someRepo */ - $someRepo = $repository; - foreach ($someRepo->findAll() as $entity) { + foreach ($repository2->findAll() as $entity) { if ($entity instanceof Author) { $this->takeAuthor($entity); } else {