Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
services:
-
class: Patchlevel\EventSourcingPHPStanExtension\AggregateRootExtension
class: Patchlevel\EventSourcingPHPStanExtension\DontRecordWhenApplyingExtension
tags:
- phpstan.properties.readWriteExtension
- phpstan.restrictedMethodUsageExtension

-
class: Patchlevel\EventSourcingPHPStanExtension\DontRecordWhenApplyingExtension
class: Patchlevel\EventSourcingPHPStanExtension\DontWriteStateWhenNotApplyingRule
tags:
- phpstan.restrictedMethodUsageExtension
- phpstan.rules.rule
36 changes: 0 additions & 36 deletions src/AggregateRootExtension.php

This file was deleted.

97 changes: 97 additions & 0 deletions src/DontWriteStateWhenNotApplyingRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcingPHPStanExtension;

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use function sprintf;

final class DontWriteStateWhenNotApplyingRule implements Rule
{
public function getNodeType(): string
{
return ClassMethod::class;
}

public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof ClassReflection) {
return [];
}

if (!$classReflection->implementsInterface(AggregateRoot::class)) {
return [];
}

// check for handle attribute
$hasHandleAttribute = false;
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ((string) $attr->name === 'Handle' || str_ends_with((string) $attr->name, '\\Handle')) {
$hasHandleAttribute = true;
break 2;
}
}
}

if (!$hasHandleAttribute) {
return [];
}

// now try to find writes by traversing over the node
$nodeFinder = new \PhpParser\NodeTraverser();
$visitor = new class($node, $scope) extends NodeVisitorAbstract {
public array $errors;
private ClassMethod $method;
private Scope $scope;

public function __construct(ClassMethod $method, Scope $scope)
{
$this->errors = [];
$this->method = $method;
$this->scope = $scope;
}

public function enterNode(Node $node)
{
if ($node instanceof Assign && $node->var instanceof PropertyFetch) {
/** @var PropertyFetch $propertyFetch */
$propertyFetch = $node->var;

if (
$propertyFetch->var instanceof Node\Expr\Variable &&
($propertyFetch->var->name === 'this' || $propertyFetch->var->name === 'self')
) {
$this->errors[] = RuleErrorBuilder::message(sprintf(
'Property "%s" should not be written in #[Handle] method "%s::%s()"',
$propertyFetch->name instanceof Node\Identifier ? $propertyFetch->name->name : 'unknown',
$this->scope->getClassReflection()?->getName() ?? 'unknown',
$this->method->name->name
))
->identifier('myCustomRules.newPerson')
->build();
}
}
}
};
$nodeFinder->addVisitor($visitor);
$nodeFinder->traverse($node->getStmts() ?? []);

return $visitor->errors;
}
}
9 changes: 9 additions & 0 deletions tests/Invalid/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Aggregate\Uuid;
use Patchlevel\EventSourcing\Attribute\Apply;
use Patchlevel\EventSourcing\Attribute\Handle;
use Patchlevel\EventSourcing\Attribute\Id;
use Patchlevel\EventSourcingPHPStanExtension\Tests\Valid\ProfileCreated;

class Profile extends BasicAggregateRoot
{
#[Id]
private Uuid $id;

Check failure on line 15 in tests/Invalid/Profile.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Class Patchlevel\EventSourcingPHPStanExtension\Tests\Invalid\Profile has an uninitialized property $id. Give it default value or assign it in the constructor.
private string $name;

Check failure on line 16 in tests/Invalid/Profile.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Class Patchlevel\EventSourcingPHPStanExtension\Tests\Invalid\Profile has an uninitialized property $name. Give it default value or assign it in the constructor.

#[Handle]

Check failure on line 18 in tests/Invalid/Profile.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.5, ubuntu-latest)

Property "name" should not be written in #[Handle] method "Patchlevel\EventSourcingPHPStanExtension\Tests\Invalid\Profile::create()"
public static function create(Uuid $id, string $name): self
{
$self = new self();
$self->recordThat(new ProfileCreated($id, $name));
$self->name = 'asd';
$self->changeStateHidden();

return $self;
}
Expand All @@ -36,6 +40,11 @@
$this->recordThat(new ProfileCreated($event->id, $event->name));
}

public function changeStateHidden(): void
{
$this->name = 'invalid';
}

public function id(): Uuid
{
return $this->id;
Expand Down
Loading