Skip to content

Commit

Permalink
add support for PHPStan 2.0 (added identifiers)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Nov 15, 2024
1 parent f9291d2 commit 79c9996
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
23 changes: 12 additions & 11 deletions src/Rules/SetValueMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;


Expand All @@ -35,8 +36,6 @@ public function getNodeType(): string

/**
* @param MethodCall $node
*
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/testbox/Types/RepositoryTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand All @@ -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 {
Expand Down

0 comments on commit 79c9996

Please sign in to comment.