Skip to content

Commit f223985

Browse files
authored
Merge pull request #66 from georgeconstantinou/issue-63
feat: Support custom application namespace
2 parents c5d1169 + 449ea9b commit f223985

18 files changed

Lines changed: 112 additions & 27 deletions

extension.neon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ services:
5151
tags:
5252
- phpstan.broker.expressionTypeResolverExtension
5353
-
54-
factory: CakeDC\PHPStan\Type\BaseTraitExpressionTypeResolverExtension(Cake\ORM\Locator\LocatorAwareTrait, fetchTable, %s\Model\Table\%sTable, defaultTable)
54+
factory: CakeDC\PHPStan\Type\BaseTraitExpressionTypeResolverExtension(Cake\ORM\Locator\LocatorAwareTrait, fetchTable, %s\Model\Table\%sTable, @CakeDC\PHPStan\Utility\CakeNameRegistry, defaultTable)
5555
tags:
5656
- phpstan.broker.expressionTypeResolverExtension
5757
-
@@ -62,3 +62,7 @@ services:
6262
class: CakeDC\PHPStan\Type\SelectQueryFindListReturnTypeExtension
6363
tags:
6464
- phpstan.broker.dynamicMethodReturnTypeExtension
65+
-
66+
class: CakeDC\PHPStan\Utility\CakeNameRegistry
67+
arguments:
68+
appNamespace: %cakeDC.appNamespace%

rules.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
parameters:
22
cakeDC:
3+
appNamespace: App
34
addAssociationExistsTableClassRule: true
45
addAssociationMatchOptionsTypesRule: true
56
addBehaviorExistsClassRule: true
@@ -13,6 +14,7 @@ parameters:
1314
disallowDebugStaticCallRule: true
1415
parametersSchema:
1516
cakeDC: structure([
17+
appNamespace: string()
1618
addAssociationExistsTableClassRule: anyOf(bool(), arrayOf(bool()))
1719
addAssociationMatchOptionsTypesRule: anyOf(bool(), arrayOf(bool()))
1820
addBehaviorExistsClassRule: anyOf(bool(), arrayOf(bool()))

src/Rule/Controller/LoadComponentExistsClassRule.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,22 @@ class LoadComponentExistsClassRule extends LoadObjectExistsCakeClassRule
3838
'load',
3939
];
4040

41+
private readonly CakeNameRegistry $cakeNameRegistry;
42+
43+
/**
44+
* @param \CakeDC\PHPStan\Utility\CakeNameRegistry $cakeNameRegistry
45+
*/
46+
public function __construct(?CakeNameRegistry $cakeNameRegistry = null)
47+
{
48+
$this->cakeNameRegistry = $cakeNameRegistry ?? CakeNameRegistry::instance();
49+
}
50+
4151
/**
4252
* @inheritDoc
4353
*/
4454
protected function getTargetClassName(string $name): ?string
4555
{
46-
return CakeNameRegistry::getComponentClassName($name);
56+
return $this->cakeNameRegistry->getComponentClassName($name);
4757
}
4858

4959
/**

src/Rule/Mailer/GetMailerExistsClassRule.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class GetMailerExistsClassRule implements Rule
2929
*/
3030
protected string $identifier = 'cake.getMailer.existClass';
3131

32+
private readonly CakeNameRegistry $cakeNameRegistry;
33+
34+
/**
35+
* @param \CakeDC\PHPStan\Utility\CakeNameRegistry $cakeNameRegistry
36+
*/
37+
public function __construct(?CakeNameRegistry $cakeNameRegistry = null)
38+
{
39+
$this->cakeNameRegistry = $cakeNameRegistry ?? CakeNameRegistry::instance();
40+
}
41+
3242
/**
3343
* @inheritDoc
3444
*/
@@ -66,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array
6676
}
6777
$reflection = $callerType->getClassReflection();
6878

69-
if (CakeNameRegistry::getMailerClassName($value->value) !== null) {
79+
if ($this->cakeNameRegistry->getMailerClassName($value->value) !== null) {
7080
return [];
7181
}
7282

src/Rule/Model/AddAssociationExistsTableClassRule.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,22 @@ class AddAssociationExistsTableClassRule extends LoadObjectExistsCakeClassRule
4242
*/
4343
protected array $associationCollectionMethods = ['load'];
4444

45+
private readonly CakeNameRegistry $cakeNameRegistry;
46+
47+
/**
48+
* @param \CakeDC\PHPStan\Utility\CakeNameRegistry $cakeNameRegistry
49+
*/
50+
public function __construct(?CakeNameRegistry $cakeNameRegistry = null)
51+
{
52+
$this->cakeNameRegistry = $cakeNameRegistry ?? CakeNameRegistry::instance();
53+
}
54+
4555
/**
4656
* @inheritDoc
4757
*/
4858
protected function getTargetClassName(string $name): ?string
4959
{
50-
return CakeNameRegistry::getTableClassName($name);
60+
return $this->cakeNameRegistry->getTableClassName($name);
5161
}
5262

5363
/**

src/Rule/Model/AddBehaviorExistsClassRule.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,22 @@ class AddBehaviorExistsClassRule extends LoadObjectExistsCakeClassRule
3838
'load',
3939
];
4040

41+
private readonly CakeNameRegistry $cakeNameRegistry;
42+
43+
/**
44+
* @param \CakeDC\PHPStan\Utility\CakeNameRegistry $cakeNameRegistry
45+
*/
46+
public function __construct(?CakeNameRegistry $cakeNameRegistry = null)
47+
{
48+
$this->cakeNameRegistry = $cakeNameRegistry ?? CakeNameRegistry::instance();
49+
}
50+
4151
/**
4252
* @inheritDoc
4353
*/
4454
protected function getTargetClassName(string $name): ?string
4555
{
46-
return CakeNameRegistry::getBehaviorClassName($name);
56+
return $this->cakeNameRegistry->getBehaviorClassName($name);
4757
}
4858

4959
/**

src/Traits/BaseCakeRegistryReturnTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CakeDC\PHPStan\Traits;
1515

16-
use CakeDC\PHPStan\Utility\CakeNameRegistry;
1716
use PhpParser\Node\Expr\MethodCall;
1817
use PHPStan\Analyser\Scope;
1918
use PHPStan\Reflection\MethodReflection;
@@ -75,7 +74,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
7574
*/
7675
protected function getCakeType(string $baseName): ObjectType
7776
{
78-
$className = CakeNameRegistry::getClassName($baseName, $this->namespaceFormat);
77+
$className = $this->cakeNameRegistry->getClassName($baseName, $this->namespaceFormat);
7978
if ($className !== null) {
8079
return new ObjectType($className);
8180
}

src/Type/BaseTraitExpressionTypeResolverExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function __construct(
4141
protected string $targetTrait,
4242
protected string $methodName,
4343
protected string $namespaceFormat,
44+
protected CakeNameRegistry $cakeNameRegistry,
4445
protected ?string $propertyDefaultValue = null,
4546
) {
4647
}
@@ -74,7 +75,7 @@ public function getType(Expr $expr, Scope $scope): ?Type
7475
if ($baseName === null) {
7576
return null;
7677
}
77-
$className = CakeNameRegistry::getClassName($baseName, $this->namespaceFormat);
78+
$className = $this->cakeNameRegistry->getClassName($baseName, $this->namespaceFormat);
7879
if ($className !== null) {
7980
return new ObjectType($className);
8081
}

src/Type/ComponentLoadDynamicReturnTypeExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\Controller\Component;
1717
use Cake\Controller\Controller;
1818
use CakeDC\PHPStan\Traits\BaseCakeRegistryReturnTrait;
19+
use CakeDC\PHPStan\Utility\CakeNameRegistry;
1920
use PHPStan\Type\DynamicMethodReturnTypeExtension;
2021

2122
class ComponentLoadDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -40,14 +41,17 @@ class ComponentLoadDynamicReturnTypeExtension implements DynamicMethodReturnType
4041
*/
4142
protected string $namespaceFormat;
4243

44+
private readonly CakeNameRegistry $cakeNameRegistry;
45+
4346
/**
4447
* TableLocatorDynamicReturnTypeExtension constructor.
4548
*/
46-
public function __construct()
49+
public function __construct(?CakeNameRegistry $cakeNameRegistry = null)
4750
{
4851
$this->className = Controller::class;
4952
$this->methodName = 'loadComponent';
5053
$this->defaultClass = Component::class;
5154
$this->namespaceFormat = '%s\\Controller\Component\\%sComponent';
55+
$this->cakeNameRegistry = $cakeNameRegistry ?? CakeNameRegistry::instance();
5256
}
5357
}

src/Type/ConsoleHelperLoadDynamicReturnTypeExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\Console\ConsoleIo;
1717
use Cake\Console\Helper;
1818
use CakeDC\PHPStan\Traits\BaseCakeRegistryReturnTrait;
19+
use CakeDC\PHPStan\Utility\CakeNameRegistry;
1920
use PHPStan\Type\DynamicMethodReturnTypeExtension;
2021
use PHPStan\Type\Type;
2122

@@ -42,15 +43,18 @@ class ConsoleHelperLoadDynamicReturnTypeExtension implements DynamicMethodReturn
4243
*/
4344
protected string $namespaceFormat;
4445

46+
private readonly CakeNameRegistry $cakeNameRegistry;
47+
4548
/**
4649
* TableLocatorDynamicReturnTypeExtension constructor.
4750
*/
48-
public function __construct()
51+
public function __construct(?CakeNameRegistry $cakeNameRegistry = null)
4952
{
5053
$this->className = ConsoleIo::class;
5154
$this->methodName = 'helper';
5255
$this->defaultClass = Helper::class;
5356
$this->namespaceFormat = '%s\\Command\Helper\\%sHelper';
57+
$this->cakeNameRegistry = $cakeNameRegistry ?? CakeNameRegistry::instance();
5458
}
5559

5660
/**

0 commit comments

Comments
 (0)