Skip to content

Commit

Permalink
Extended RequiredSetterHandler to also check for attributes (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
UlrichThomasGabor committed Mar 22, 2023
1 parent 64ef8b5 commit 08b1cd7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
55 changes: 40 additions & 15 deletions src/Handler/RequiredSetterHandler.php
Expand Up @@ -10,6 +10,7 @@
use Psalm\Internal\PhpVisitor\AssignmentMapVisitor;
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
use Psalm\Storage\ClassLikeStorage;

class RequiredSetterHandler implements AfterClassLikeVisitInterface
{
Expand All @@ -18,32 +19,56 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
$stmt = $event->getStmt();
$storage = $event->getStorage();

$stmt_source = $event->getStatementsSource();
$aliases = $stmt_source->getAliases();

if (!$stmt instanceof Class_) {
return;
}

foreach ($stmt->getMethods() as $method) {
// Check for PhpDoc annotation
$docComment = $method->getDocComment();

if ($docComment instanceof Doc && false !== strpos($docComment->getText(), '@required')) {
$traverser = new NodeTraverser();
$visitor = new AssignmentMapVisitor(null);
$traverser->addVisitor($visitor);
$traverser->traverse($method->getStmts() ?? []);

foreach (array_keys($visitor->getAssignmentMap()) as $assignment) {
if (0 !== strpos($assignment, '$this->')) {
continue;
}
self::markAsInitializedProperties($storage, $method->getStmts() ?? []);
}

$property = substr($assignment, strlen('$this->'));
if (!array_key_exists($property, $storage->properties)) {
continue;
// Check for attribute annotation
foreach ($method->getAttrGroups() as $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
/** @var lowercase-string $lcName */
$lcName = $attribute->name->toLowerString();
if (array_key_exists($lcName, $aliases->uses)) {
$name = $aliases->uses[$lcName];
} else {
$name = $attribute->name->toString();
}
if ('Symfony\Contracts\Service\Attribute\Required' === $name) {
self::markAsInitializedProperties($storage, $method->getStmts() ?? []);
}

$storage->initialized_properties[$property] = true;
}
}
}
}

private static function markAsInitializedProperties(ClassLikeStorage $storage, array $stmts): void
{
$traverser = new NodeTraverser();
$visitor = new AssignmentMapVisitor(null);
$traverser->addVisitor($visitor);
$traverser->traverse($stmts);

foreach (array_keys($visitor->getAssignmentMap()) as $assignment) {
if (0 !== strpos($assignment, '$this->')) {
continue;
}

$property = substr($assignment, strlen('$this->'));
if (!array_key_exists($property, $storage->properties)) {
continue;
}

$storage->initialized_properties[$property] = true;
}
}
}
41 changes: 41 additions & 0 deletions tests/acceptance/acceptance/RequiredSetter.feature
Expand Up @@ -44,3 +44,44 @@ Feature: Annotation class
| Type | Message |
| PropertyNotSetInConstructor | Property MyServiceB::$a is not defined in constructor of MyServiceB or in any private or final methods called in the constructor |
And I see no other errors

Scenario: PropertyNotSetInConstructor error is not raised when the required attribute is present (with use).
Given I have the following code
"""
<?php
use \Symfony\Contracts\Service\Attribute\Required;
final class MyServiceA {
}
final class MyServiceB {
private MyServiceA $a;
public function __construct(){}
#[Required]
private function setMyServiceA(MyServiceA $a): void { $this->a = $a; }
}
"""
When I run Psalm
Then I see no errors


Scenario: PropertyNotSetInConstructor error is not raised when the required attribute is present (without use).
Given I have the following code
"""
<?php
final class MyServiceA {
}
final class MyServiceB {
private MyServiceA $a;
public function __construct(){}
#[\Symfony\Contracts\Service\Attribute\Required]
private function setMyServiceA(MyServiceA $a): void { $this->a = $a; }
}
"""
When I run Psalm
Then I see no errors

0 comments on commit 08b1cd7

Please sign in to comment.