Skip to content

Commit 596fcae

Browse files
authored
Merge pull request #467 from goaop/feature/phpstan-issues-fixing
[Feature] Bump PHPStan level to 4
2 parents a774bb7 + e5fb4ca commit 596fcae

19 files changed

+167
-351
lines changed

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
parameters:
2-
level: 1
2+
level: 4
33
paths:
44
- src

src/Aop/Framework/AbstractJoinpoint.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
abstract class AbstractJoinpoint implements Joinpoint
3535
{
3636
/**
37-
* List of advices
37+
* List of advices (interceptors)
3838
*
39-
* @var array<Advice|Interceptor>
39+
* NB: All current children assume that each advice is Interceptor now.
40+
* Whereas, it isn't correct logically, this can be used to satisfy PHPStan check now.
41+
*
42+
* @var array<Interceptor>
4043
*/
4144
protected array $advices = [];
4245

@@ -58,7 +61,7 @@ abstract class AbstractJoinpoint implements Joinpoint
5861
/**
5962
* Initializes list of advices for current joinpoint
6063
*
61-
* @param array<Advice|Interceptor> $advices List of advices
64+
* @param array<Interceptor> $advices List of advices (interceptors)
6265
*/
6366
public function __construct(array $advices)
6467
{
@@ -110,11 +113,7 @@ public static function flatAndSortAdvices(array $advices): array
110113
$flattenAdvices = [];
111114
foreach ($advices as $type => $typedAdvices) {
112115
foreach ($typedAdvices as $name => $concreteAdvices) {
113-
if (is_array($concreteAdvices)) {
114-
$flattenAdvices[$type][$name] = array_keys(self::sortAdvices($concreteAdvices));
115-
} else {
116-
$flattenAdvices[$type][$name] = $concreteAdvices;
117-
}
116+
$flattenAdvices[$type][$name] = array_keys(self::sortAdvices($concreteAdvices));
118117
}
119118
}
120119

src/Aop/Framework/ClassFieldAccess.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Go\Aop\AspectException;
1616
use Go\Aop\Intercept\FieldAccess;
1717
use Go\Aop\Support\AnnotatedReflectionProperty;
18-
use ReflectionProperty;
1918

2019
use function get_class;
2120

@@ -32,7 +31,7 @@ final class ClassFieldAccess extends AbstractJoinpoint implements FieldAccess
3231
/**
3332
* Instance of reflection property
3433
*/
35-
protected ReflectionProperty $reflectionProperty;
34+
protected AnnotatedReflectionProperty $reflectionProperty;
3635

3736
/**
3837
* New value to set
@@ -80,9 +79,9 @@ public function getAccessType(): int
8079
/**
8180
* Gets the field being accessed.
8281
*
83-
* @return AnnotatedReflectionProperty the property which is being accessed.
82+
* Covariant return type is used
8483
*/
85-
public function getField(): ReflectionProperty
84+
public function getField(): AnnotatedReflectionProperty
8685
{
8786
return $this->reflectionProperty;
8887
}

src/Aop/Pointcut/AndPointcut.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Go\Aop\Pointcut;
1616
use Go\Aop\Support\AndPointFilter;
17+
use ReflectionClass;
1718
use ReflectionMethod;
1819
use ReflectionProperty;
1920

@@ -76,11 +77,12 @@ public function getKind(): int
7677
/**
7778
* Checks if point filter matches the point
7879
*
79-
* @param Pointcut $pointcut
80-
* @param ReflectionMethod|ReflectionProperty $point
81-
* @param mixed $context Related context, can be class or namespace
82-
* @param object|string|null $instance [Optional] Instance for dynamic matching
83-
* @param array|null $arguments [Optional] Extra arguments for dynamic matching
80+
* @param Pointcut $pointcut
81+
* @param ReflectionMethod|ReflectionProperty|ReflectionClass $point
82+
* @param mixed $context Related context, can be class or namespace
83+
* @param object|string|null $instance [Optional] Instance for dynamic matching
84+
* @param array|null $arguments [Optional] Extra arguments for dynamic
85+
* matching
8486
*
8587
* @return bool
8688
*/

src/Aop/Support/DeclareParentsAdvisor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
use Go\Aop\IntroductionAdvisor;
1616
use Go\Aop\IntroductionInfo;
17+
use Go\Aop\Pointcut;
1718
use Go\Aop\Pointcut\PointcutClassFilterTrait;
18-
use Go\Aop\PointFilter;
1919

2020
/**
2121
* Introduction advisor delegating to the given object.
@@ -27,9 +27,9 @@ class DeclareParentsAdvisor extends AbstractGenericAdvisor implements Introducti
2727
/**
2828
* Creates an advisor for declaring mixins via trait and interface.
2929
*/
30-
public function __construct(PointFilter $classFilter, IntroductionInfo $info)
30+
public function __construct(Pointcut $pointcut, IntroductionInfo $info)
3131
{
32-
$this->classFilter = $classFilter;
32+
$this->classFilter = $pointcut->getClassFilter();
3333
parent::__construct($info);
3434
}
3535
}

src/Bridge/Doctrine/MetadataLoadInterceptor.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44
/*
55
* Go! AOP framework
66
*
@@ -9,6 +9,7 @@
99
* This source file is subject to the license that is bundled
1010
* with this source code in the file LICENSE.
1111
*/
12+
1213
namespace Go\Bridge\Doctrine;
1314

1415
use Doctrine\Common\EventSubscriber;
@@ -52,16 +53,13 @@ public function getSubscribedEvents(): array
5253
*/
5354
public function loadClassMetadata(LoadClassMetadataEventArgs $args): void
5455
{
55-
/**
56-
* @var ClassMetadata $metadata
57-
*/
5856
$metadata = $args->getClassMetadata();
5957

6058
if (1 === preg_match(sprintf('/.+(%s)$/', AspectContainer::AOP_PROXIED_SUFFIX), $metadata->name)) {
61-
$metadata->isMappedSuperclass = true;
62-
$metadata->isEmbeddedClass = false;
63-
$metadata->table = [];
64-
$metadata->customRepositoryClassName = null;
59+
$metadata->isMappedSuperclass = true;
60+
$metadata->isEmbeddedClass = false;
61+
$metadata->table = [];
62+
$metadata->customRepositoryClassName = null;
6563

6664
$this->removeMappingsFromTraits($metadata);
6765
}
@@ -97,10 +95,11 @@ private function removeMappingsFromTraits(ClassMetadata $metadata): void
9795
/**
9896
* Get ALL traits used by one class.
9997
*
100-
* This method is copied from https://github.com/RunOpenCode/traitor-bundle/blob/master/src/RunOpenCode/Bundle/Traitor/Utils/ClassUtils.php
98+
* This method is copied from
99+
* https://github.com/RunOpenCode/traitor-bundle/blob/master/src/RunOpenCode/Bundle/Traitor/Utils/ClassUtils.php
101100
*
102-
* @param object|string $className Instance of class or FQCN
103-
* @param bool $autoload Weather to autoload class.
101+
* @param class-string $className FQCN
102+
* @param bool $autoload Weather to autoload class.
104103
*
105104
* @throws InvalidArgumentException
106105
* @throws RuntimeException
@@ -114,8 +113,9 @@ private function getTraits(string $className, bool $autoload = true): array
114113
$traits = [];
115114
// Get traits of all parent classes
116115
do {
117-
$traits = array_merge(class_uses($className, $autoload), $traits);
118-
} while ($className = get_parent_class($className));
116+
$traits = array_merge(class_uses($className, $autoload), $traits);
117+
$className = get_parent_class($className);
118+
} while ($className);
119119

120120
$traitsToSearch = $traits;
121121

src/Core/AbstractAspectLoaderExtension.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Dissect\Lexer\TokenStream\TokenStream;
1818
use Dissect\Parser\Exception\UnexpectedTokenException;
1919
use Dissect\Parser\Parser;
20+
use Doctrine\Common\Annotations\Reader;
2021
use Go\Aop\Aspect;
2122
use Go\Aop\Pointcut;
2223
use Go\Aop\PointFilter;
@@ -39,13 +40,19 @@ abstract class AbstractAspectLoaderExtension implements AspectLoaderExtension
3940
*/
4041
protected Parser $pointcutParser;
4142

43+
/**
44+
* Instance of annotation reader
45+
*/
46+
protected Reader $reader;
47+
4248
/**
4349
* Default loader constructor that accepts pointcut lexer and parser
4450
*/
45-
public function __construct(Lexer $pointcutLexer, Parser $pointcutParser)
51+
public function __construct(Lexer $pointcutLexer, Parser $pointcutParser, Reader $reader)
4652
{
4753
$this->pointcutLexer = $pointcutLexer;
4854
$this->pointcutParser = $pointcutParser;
55+
$this->reader = $reader;
4956
}
5057

5158
/**

src/Core/AdviceMatcher.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(bool $isInterceptFunctions = false)
4949
*
5050
* @param Aop\Advisor[] $advisors List of advisor to match
5151
*
52-
* @return Aop\Advice[] List of advices for class
52+
* @return Aop\Advice[][][] List of advices for function
5353
*/
5454
public function getAdvicesForFunctions(ReflectionFileNamespace $namespace, array $advisors): array
5555
{
@@ -81,7 +81,7 @@ public function getAdvicesForFunctions(ReflectionFileNamespace $namespace, array
8181
*
8282
* @param array|Aop\Advisor[] $advisors List of advisor to match
8383
*
84-
* @return Aop\Advice[][] List of advices for class
84+
* @return Aop\Advice[][][] List of advices for class
8585
*/
8686
public function getAdvicesForClass(ReflectionClass $class, array $advisors): array
8787
{
@@ -103,7 +103,7 @@ public function getAdvicesForClass(ReflectionClass $class, array $advisors): arr
103103

104104
if ($advisor instanceof IntroductionAdvisor) {
105105
if ($advisor->getClassFilter()->matches($class)) {
106-
$classAdvices[] = $this->getIntroductionFromAdvisor($originalClass, $advisor, $advisorId);
106+
$classAdvices[] = $this->getIntroductionFromAdvisor($originalClass, $advisor);
107107
}
108108
}
109109
}
@@ -172,11 +172,12 @@ private function getAdvicesFromAdvisor(
172172

173173
/**
174174
* Returns list of introduction advices from advisor
175+
*
176+
* @return Aop\IntroductionInfo[][][]
175177
*/
176178
private function getIntroductionFromAdvisor(
177179
ReflectionClass $class,
178-
IntroductionAdvisor $advisor,
179-
string $advisorId
180+
IntroductionAdvisor $advisor
180181
): array {
181182
$classAdvices = [];
182183
// Do not make introduction for traits
@@ -190,13 +191,13 @@ private function getIntroductionFromAdvisor(
190191
if (!empty($introducedTrait)) {
191192
$introducedTrait = '\\' . ltrim($introducedTrait, '\\');
192193

193-
$classAdvices[AspectContainer::INTRODUCTION_TRAIT_PREFIX][$advisorId] = $introducedTrait;
194+
$classAdvices[AspectContainer::INTRODUCTION_TRAIT_PREFIX]['root'][$introducedTrait] = $introduction;
194195
}
195196
$introducedInterface = $introduction->getInterface();
196197
if (!empty($introducedInterface)) {
197198
$introducedInterface = '\\' . ltrim($introducedInterface, '\\');
198199

199-
$classAdvices[AspectContainer::INTRODUCTION_INTERFACE_PREFIX][$advisorId] = $introducedInterface;
200+
$classAdvices[AspectContainer::INTRODUCTION_INTERFACE_PREFIX]['root'][$introducedInterface] = $introduction;
200201
}
201202

202203
return $classAdvices;

src/Core/AdviceMatcherInterface.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/*
35
* Go! AOP framework
46
*
@@ -25,7 +27,7 @@ interface AdviceMatcherInterface
2527
*
2628
* @param Advisor[] $advisors List of advisor to match
2729
*
28-
* @return Advice[][] List of advices for function
30+
* @return Advice[][][] List of advices for function
2931
*/
3032
public function getAdvicesForFunctions(ReflectionFileNamespace $namespace, array $advisors): array;
3133

@@ -34,7 +36,7 @@ public function getAdvicesForFunctions(ReflectionFileNamespace $namespace, array
3436
*
3537
* @param Advisor[] $advisors List of advisor to match
3638
*
37-
* @return Advice[][] List of advices for class
39+
* @return Advice[][][] List of advices for class
3840
*/
3941
public function getAdvicesForClass(ReflectionClass $class, array $advisors): array;
4042
}

0 commit comments

Comments
 (0)