Skip to content

Commit 359207a

Browse files
authored
Fixed "type mismatch" errors
2 parents 67a5d91 + c6617f6 commit 359207a

14 files changed

+163
-375
lines changed

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "okapi/aop",
33
"description": "PHP AOP is a PHP library that provides a powerful Aspect Oriented Programming (AOP) implementation for PHP.",
4-
"version": "1.2.10",
54
"type": "library",
65
"homepage": "https://github.com/okapi-web/php-aop",
76
"license": "MIT",

src/AopKernel.php

-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Okapi\Aop\Core\Container\{AspectManager, TransformerManager};
1515
use Okapi\Aop\Core\Options;
1616
use Okapi\Aop\Core\Processor\AspectProcessor;
17-
use Okapi\Aop\Core\Transformer\NetteReflectionWithBetterReflection;
1817
use Okapi\CodeTransformer\CodeTransformerKernel;
1918
use Okapi\CodeTransformer\Core\AutoloadInterceptor\ClassLoader as CodeTransformerClassLoader;
2019
use Okapi\CodeTransformer\Core\Cache\CachePaths as CodeTransformerCachePaths;
@@ -110,11 +109,6 @@ protected static function registerDependencyInjection(): void
110109
*/
111110
protected function preInit(): void
112111
{
113-
// Add internal transformers
114-
$this->transformerManager->addTransformers([
115-
NetteReflectionWithBetterReflection::class,
116-
]);
117-
118112
// Add the aspects
119113
$this->aspectManager->addAspects($this->aspects);
120114

src/Attributes/After.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* This attribute is used to mark a method as an after advice.
1212
*/
13-
#[Attribute(Attribute::TARGET_METHOD)]
13+
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
1414
class After extends MethodAdvice
1515
{
1616
}

src/Attributes/Around.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* This attribute is used to mark a method as an around advice.
1212
*/
13-
#[Attribute(Attribute::TARGET_METHOD)]
13+
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
1414
class Around extends MethodAdvice
1515
{
1616
}

src/Attributes/Before.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* This attribute is used to mark a method as a before advice.
1212
*/
13-
#[Attribute(Attribute::TARGET_METHOD)]
13+
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
1414
class Before extends MethodAdvice
1515
{
1616
}

src/Core/.phpstorm.meta.php

-48
This file was deleted.

src/Core/Transform/ProxiedClassModifier.php

-54
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,6 @@ private function changeVisibility(): void
203203
private function replaceSelfType(): void
204204
{
205205
$this->nodeCallbacks[] = function (Node $node) {
206-
// Replace parameter object types with the proxied class name
207-
if ($node instanceof Node\Parameter
208-
&& $node->typeDeclarationList instanceof Node\DelimitedList\QualifiedNameList
209-
) {
210-
foreach ($node->typeDeclarationList->children as $type) {
211-
if ($type instanceof Node\QualifiedName) {
212-
$this->replaceParameterSelfType($type);
213-
}
214-
}
215-
}
216-
217206
// Replace return object types with the proxied class name
218207
if ($node instanceof Node\MethodDeclaration
219208
&& $node->returnTypeList instanceof Node\DelimitedList\QualifiedNameList
@@ -234,49 +223,6 @@ private function replaceSelfType(): void
234223
};
235224
}
236225

237-
/**
238-
* Replace the parameter self type with the proxied class name.
239-
*
240-
* @param Node\QualifiedName $qualifiedName
241-
*
242-
* @return void
243-
*
244-
* @noinspection PhpDocMissingThrowsInspection
245-
*/
246-
private function replaceParameterSelfType(
247-
Node\QualifiedName $qualifiedName,
248-
): void {
249-
$typeText = $qualifiedName->getText();
250-
251-
// Self
252-
if ($typeText === 'self') {
253-
$this->edit(
254-
$qualifiedName,
255-
$this->proxiedClassName,
256-
);
257-
258-
return;
259-
}
260-
261-
// Other classes that have a proxy
262-
/** @noinspection PhpUnhandledExceptionInspection */
263-
$fullClassName = '\\' . $qualifiedName->getResolvedName()
264-
->getFullyQualifiedNameText();
265-
$proxyClassName = $fullClassName
266-
. $this->cachePaths::PROXIED_SUFFIX;
267-
268-
// Autoload the class with class_exists,
269-
// so we can check if the proxy exists
270-
if (class_exists($fullClassName)
271-
&& class_exists($proxyClassName)
272-
) {
273-
$this->edit(
274-
$qualifiedName,
275-
$proxyClassName,
276-
);
277-
}
278-
}
279-
280226
/**
281227
* Replace the return self type with the proxied class name.
282228
*

src/Core/Transform/WovenClassBuilder.php

+22-76
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
use Nette\PhpGenerator\ClassType;
88
use Nette\PhpGenerator\Factory;
99
use Nette\PhpGenerator\Method;
10-
use Nette\PhpGenerator\Parameter;
1110
use Nette\PhpGenerator\PhpNamespace;
11+
use Nette\PhpGenerator\PromotedParameter;
1212
use Nette\PhpGenerator\Property;
13-
use Nette\Utils\Type;
1413
use Okapi\Aop\Core\Cache\CachePaths;
1514
use Okapi\Aop\Core\Container\AdviceContainer;
1615
use Okapi\Aop\Core\Container\AdviceType\MethodAdviceContainer;
1716
use Okapi\Aop\Core\JoinPoint\JoinPoint;
1817
use Okapi\Aop\Core\JoinPoint\JoinPointInjector;
1918
use Okapi\CodeTransformer\Core\DI;
2019
use Okapi\CodeTransformer\Transformer\Code;
20+
use Roave\BetterReflection\Reflection\Adapter\ReflectionMethod;
2121
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
2222

2323
/**
@@ -206,24 +206,31 @@ private function buildMethods(): array
206206
* @param BetterReflectionMethod $refMethod
207207
*
208208
* @return Method
209+
*
210+
* @noinspection PhpDocMissingThrowsInspection
209211
*/
210212
private function buildMethod(BetterReflectionMethod $refMethod): Method
211213
{
212-
$method = (new Factory)->fromMethodReflection($refMethod);
214+
/** @noinspection PhpUnhandledExceptionInspection */
215+
$method = (new Factory)->fromMethodReflection(
216+
new ReflectionMethod($refMethod),
217+
);
218+
213219
$methodName = $refMethod->getName();
214220

215-
// Replace parameter and return types with the proxied class
216-
$declaringClass = $refMethod->getDeclaringClass();
217-
$fullClassName = '\\' . $declaringClass->getNamespaceName()
218-
. '\\' . $declaringClass->getShortName();
219-
$proxiedClassName = $fullClassName . $this->cachePaths::PROXIED_SUFFIX;
220-
foreach ($method->getParameters() as $parameter) {
221-
$this->replaceParameterType($parameter, $proxiedClassName);
222-
}
223-
if ($method->getReturnType() === 'self') {
224-
$method->setReturnType(
225-
$proxiedClassName,
226-
);
221+
// ReadOnly Hack: https://github.com/nette/php-generator/issues/158
222+
foreach ($refMethod->getParameters() as $refParameter) {
223+
if ($refParameter->isPromoted()
224+
&& ($declaringClass = $refParameter->getDeclaringClass())
225+
&& ($refParameterName = $refParameter->getName())
226+
&& $declaringClass->hasProperty($refParameterName)
227+
&& ($property = $declaringClass->getProperty($refParameterName))
228+
&& $property->isReadOnly()
229+
) {
230+
/** @var PromotedParameter $parameter */
231+
$parameter = $method->getParameter($refParameterName);
232+
$parameter->setReadOnly();
233+
}
227234
}
228235

229236
// Add "return" if the method has a return type
@@ -260,67 +267,6 @@ private function buildMethod(BetterReflectionMethod $refMethod): Method
260267
return $method;
261268
}
262269

263-
/**
264-
* Replace the parameter type with the proxied class.
265-
*
266-
* @param Parameter|Type $parameterOrType
267-
* @param string $proxiedClassName
268-
*
269-
* @return void
270-
*/
271-
private function replaceParameterType(
272-
Parameter|Type $parameterOrType,
273-
string $proxiedClassName
274-
): void {
275-
$objectType = $parameterOrType instanceof Parameter
276-
? $parameterOrType->getType(true)
277-
: $parameterOrType;
278-
if (!$objectType) {
279-
return;
280-
}
281-
282-
$typeString = $this->getTypeString($objectType, $proxiedClassName);
283-
$parameterOrType->setType($typeString);
284-
}
285-
286-
/**
287-
* Get the parameter array as a string.
288-
*
289-
* @param Type $type
290-
* @param string $proxiedClassName
291-
*
292-
* @return string
293-
*/
294-
private function getTypeString(Type $type, string $proxiedClassName): string
295-
{
296-
// If the type is a union or intersection, we need to replace each type
297-
if ($type->isUnion() || $type->isIntersection()) {
298-
$typeNames = array_map(function ($type) use ($proxiedClassName) {
299-
return $this->getTypeString($type, $proxiedClassName);
300-
}, $type->getTypes());
301-
$glue = $type->isUnion() ? '|' : '&';
302-
return implode($glue, $typeNames);
303-
} elseif ($type->getSingleName() === 'self') {
304-
// If the type is "self", we need to replace it with the proxied
305-
// class
306-
return $proxiedClassName;
307-
} elseif ($type->isClass()) {
308-
// If the type is a class, we need to check if the class
309-
// is a proxy
310-
$typeFullClassName = '\\' . $type->getSingleName();
311-
$typeProxiedClassName =
312-
$typeFullClassName . $this->cachePaths::PROXIED_SUFFIX;
313-
314-
if (class_exists($typeFullClassName)
315-
&& class_exists($typeProxiedClassName)
316-
) {
317-
return $typeProxiedClassName;
318-
}
319-
}
320-
321-
return $type->getSingleName();
322-
}
323-
324270
/**
325271
* Create an associative array with the parameter name as key and the
326272
* parameter as value.

0 commit comments

Comments
 (0)