Skip to content

Commit

Permalink
Merge pull request #5 from profuel/patch-1
Browse files Browse the repository at this point in the history
Improve auto-resolve class patterns
  • Loading branch information
vitaliiivanovspryker authored Jan 6, 2025
2 parents b2c0df4 + a120d06 commit efa0e56
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/Project/Common/InstanceResolvingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class InstanceResolvingRule extends AbstractRule implements ClassAware
/**
* @var string
*/
public const RULE = 'Repository|EntityManager|QueryContainer|Facade|DependencyProvider|Client|Service instances can not be initialized directly with "new". Use Dependency Provider and Resolvers';
public const RULE = 'Automatically resolved instances must not be initialized directly with "new". Use Dependency Provider and Resolvers.';

/**
* @return string
Expand All @@ -29,7 +29,15 @@ public function getDescription(): string
/**
* @var string
*/
protected const INSTANCE_PATTERN = '([\w]+(Repository|EntityManager|QueryContainer|Facade|DependencyProvider|Client|Service)$)';
protected const INSTANCE_PATTERNS = [
'/^(\w+)\\\\\Zed\\\\\w+\\\\Persistence\\\\\w+(Repository|EntityManager|QueryContainer|PersistenceFactory)$/',
'/^(\w+)\\\\\Zed\\\\\w+\\\\Business\\\\\w+(Facade|BusinessFactory)$/',
'/^(\w+)\\\\\Zed\\\\\w+\\\\Communication\\\\\w+(CommunicationFactory)$/',
'/^(\w+)\\\\\Zed\\\\\w+\\\\\w+(Config|DependencyProvider)$/',
'/^(\w+)\\\\\Client\\\\\w+\\\\\w+(Client|Config|DependencyProvider)$/',
'/^(\w+)\\\\\Service\\\\\w+\\\\\w+(DependencyProvider|Service)$/',
'/^(\w+)\\\\\Glue\\\\\w+\\\\\w+(Factory|Config|DependencyProvider)$/',
];

/**
* @param \PHPMD\AbstractNode $node
Expand All @@ -55,14 +63,16 @@ public function apply(AbstractNode $node): void

$referenceName = trim($reference->getName(), '\\');

if (preg_match(static::INSTANCE_PATTERN, $referenceName)) {
$message = sprintf(
'Entity `%s` is initialized in method `%s`. %s',
$referenceName,
$methodName,
static::RULE,
);
$this->addViolation($methodNode, [$message]);
foreach (static::INSTANCE_PATTERNS as $pattern) {
if (preg_match($pattern, $referenceName)) {
$message = sprintf(
'Entity `%s` is initialized in method `%s`. %s',
$referenceName,
$methodName,
static::RULE,
);
$this->addViolation($methodNode, [$message]);
}
}
}
}
Expand Down

0 comments on commit efa0e56

Please sign in to comment.