Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NoValueObjectInServiceConstructorRule #152

Merged
merged 1 commit into from
Dec 17, 2024
Merged
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
5 changes: 5 additions & 0 deletions src/Enum/RuleIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,9 @@ final class RuleIdentifier
* @var string
*/
public const SYMFONY_REQUIRE_INVOKABLE_CONTROLLER = 'symfony.requireInvokableController';

/**
* @var string
*/
public const NO_VALUE_OBJECT_IN_SERVICE_CONSTRUCTOR = 'symplify.noValueObjectInServiceConstructor';
}
72 changes: 72 additions & 0 deletions src/Rules/NoValueObjectInServiceConstructorRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Rules;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\RuleIdentifier;

/**
* @implements Rule<ClassMethod>
*/
final class NoValueObjectInServiceConstructorRule implements Rule
{
public function getNodeType(): string
{
return ClassMethod::class;
}

/**
* @param ClassMethod $node
*/
public function processNode(Node $node, Scope $scope): array
{
if ($node->name->toString() !== '__construct') {
return [];
}

if (! $scope->isInClass()) {
return [];
}

$classReflection = $scope->getClassReflection();

// value objects can accept value objects
if ($this->isValueObject($classReflection->getName())) {
return [];
}

$ruleErrors = [];

foreach ($node->params as $param) {
if (! $param->type instanceof Name) {
continue;
}

$paramType = $param->type->toString();
if (! $this->isValueObject($paramType)) {
continue;
}

$ruleErrors[] = RuleErrorBuilder::message(sprintf(
'Value object "%s" cannot be passed to constructor of a service. Pass it as a method argument instead',
$paramType
))
->identifier(RuleIdentifier::NO_VALUE_OBJECT_IN_SERVICE_CONSTRUCTOR)
->build();
}

return $ruleErrors;
}

private function isValueObject(string $className): bool
{
return preg_match('#(ValueObject|DataObject|Models)#', $className) === 1;
}
}
Loading