Skip to content

Commit

Permalink
Merge branch '3.x' into dependabot/composer/doctrine/annotations-tw-1…
Browse files Browse the repository at this point in the history
….11.1or-tw-2.0.0
  • Loading branch information
lisachenko authored Feb 7, 2024
2 parents f883097 + 45c19c9 commit 19f65c7
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 18 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

"require": {
"php": "^7.4.0",
"doctrine/annotations": "^1.11.1 || ^2.0.0",
"doctrine/annotations": "^2.0",
"doctrine/cache": "^1.10",
"goaop/parser-reflection": "^3.0.1",
"jakubledl/dissect": "~1.0",
Expand Down Expand Up @@ -54,7 +54,8 @@
"Go\\Tests\\TestProject\\": "tests/Fixtures/project/src/"
},
"files": [
"tests/functions.php"
"tests/functions.php",
"tests/Go/Stubs/ClassWithoutNamespace.php"
]
},

Expand Down
2 changes: 1 addition & 1 deletion src/Aop/Framework/AbstractInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ abstract class AbstractInterceptor implements Interceptor, OrderedAdvice, Serial
/**
* Advice order
*/
private int $adviceOrder;
private int $adviceOrder = 0;

/**
* Default constructor for interceptor
Expand Down
2 changes: 1 addition & 1 deletion src/Aop/Framework/AbstractMethodInvocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class AbstractMethodInvocation extends AbstractInvocation implements Me
/**
* Instance of object for invoking
*/
protected ?object $instance;
protected ?object $instance = null;

/**
* Instance of reflection method for invocation
Expand Down
7 changes: 4 additions & 3 deletions src/Core/GoAspectContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Go\Core;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\Cache as DoctrineCache;
use Go\Aop\Advisor;
use Go\Aop\Aspect;
Expand Down Expand Up @@ -104,10 +104,11 @@ public function __construct()

$this->share('aspect.annotation.reader', function (Container $container) {
$options = $container->get('kernel.options');
$annotationCache = $container->get('aspect.annotation.cache');

return new CachedReader(
return new PsrCachedReader(
new AnnotationReader(),
$container->get('aspect.annotation.cache'),
new DoctrineCache\Psr6\CacheAdapter($annotationCache),

Check failure on line 111 in src/Core/GoAspectContainer.php

View workflow job for this annotation

GitHub Actions / build

Cannot instantiate class Doctrine\Common\Cache\Psr6\CacheAdapter via private constructor Doctrine\Common\Cache\Psr6\CacheAdapter::__construct().
$options['debug'] ?? false
);
});
Expand Down
8 changes: 0 additions & 8 deletions src/Instrument/ClassLoading/AopComposerLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Go\Instrument\PathResolver;
use Go\Instrument\Transformer\FilterInjectorTransformer;
use Composer\Autoload\ClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;

/**
* AopComposerLoader class is responsible to use a weaver for classes instead of original one
Expand Down Expand Up @@ -92,13 +91,6 @@ public static function init(array $options, AspectContainer $container): bool
foreach ($loaders as &$loader) {
$loaderToUnregister = $loader;
if (is_array($loader) && ($loader[0] instanceof ClassLoader)) {
$originalLoader = $loader[0];
// Configure library loader for doctrine annotation loader
AnnotationRegistry::registerLoader(function($class) use ($originalLoader) {
$originalLoader->loadClass($class);

return class_exists($class, false);
});
$loader[0] = new AopComposerLoader($loader[0], $container, $options);
self::$wasInitialized = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Instrument/Transformer/SelfValueVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function beforeTraverse(array $nodes)
public function enterNode(Node $node)
{
if ($node instanceof Namespace_) {
$this->namespace = $node->name->toString();
$this->namespace = !empty($node->name) ? $node->name->toString() : null;
} elseif ($node instanceof Class_) {
if ($node->name !== null) {
$this->className = new Name($node->name->toString());
Expand Down
2 changes: 1 addition & 1 deletion src/Proxy/ClassProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function __construct(

$this->generator = new ClassGenerator(
$originalClass->getShortName(),
$originalClass->getNamespaceName(),
!empty($originalClass->getNamespaceName()) ? $originalClass->getNamespaceName() : null,
$originalClass->isFinal() ? ClassGenerator::FLAG_FINAL : null,
$parentClassName,
$introducedInterfaces,
Expand Down
2 changes: 1 addition & 1 deletion src/Proxy/Part/InterceptedMethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(ReflectionMethod $reflectionMethod, string $body, bo
if ($reflectionMethod->hasReturnType()) {
$reflectionReturnType = $reflectionMethod->getReturnType();
if ($reflectionReturnType instanceof ReflectionNamedType) {
$returnTypeName = $reflectionReturnType->getName();
$returnTypeName = ($reflectionReturnType->allowsNull() ? '?' : '') . $reflectionReturnType->getName();
} else {
$returnTypeName = (string)$reflectionReturnType;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Go/Aop/Framework/AbstractMethodInvocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,41 @@ public function testProvidesAccessToAnnotations(): void
{
$this->assertInstanceOf(AnnotationAccess::class, $this->invocation->getMethod());
}

public function testInstanceIsInitialized(): void
{
$this->expectNotToPerformAssertions();
$o = new class extends AbstractMethodInvocation {

protected static string $propertyName = 'scope';
public function __construct()
{
parent::__construct([new AroundInterceptor(function () {})], '\Go\Aop\Framework\AbstractMethodInvocationTest', 'testInstanceIsInitialized');
}

public function isDynamic(): bool
{
return false;
}

public function getThis(): ?object
{
return null;
}

public function getScope(): string
{
return 'testScope';
}

public function proceed()
{
if ($this->level < 3) {
$this->__invoke('testInstance');
}
}
};

$o->__invoke('testInstance');
}
}
10 changes: 10 additions & 0 deletions tests/Go/Instrument/Transformer/SelfValueTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public function testTransformerReplacesAllSelfPlaces(): void
$expected = file_get_contents(__DIR__ . '/_files/file-with-self-transformed.php');
$this->assertSame($expected, (string) $metadata->source);
}

public function testTransformerReplacesAllSelfPlacesWithoutNamespace(): void
{
$testFile = fopen(__DIR__ . '/_files/file-with-self-no-namespace.php', 'rb');
$content = stream_get_contents($testFile);
$metadata = new StreamMetaData($testFile, $content);
$this->transformer->transform($metadata);
$expected = file_get_contents(__DIR__ . '/_files/file-with-self-no-namespace-transformed.php');
$this->assertSame($expected, (string) $metadata->source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/** @noinspection PhpIllegalPsrClassPathInspection */
declare(strict_types=1);

class ClassWithSelfNoNamespace extends \Exception
{
const CLASS_CONST = \ClassWithSelfNoNamespace::class;

private static $foo = 42;

private \ClassWithSelfNoNamespace $instance;

public function acceptsAndReturnsSelf(\ClassWithSelfNoNamespace $instance): \ClassWithSelfNoNamespace
{
return $instance;
}

public function containsClosureWithSelf()
{
$func = function (\ClassWithSelfNoNamespace $instance): \ClassWithSelfNoNamespace {
return $instance;
};
$func($this);
}

public function staticMethodCall()
{
return \ClassWithSelfNoNamespace::staticPropertyAccess();
}

public function classConstantFetch()
{
return \ClassWithSelfNoNamespace::class . \ClassWithSelfNoNamespace::CLASS_CONST;
}

public static function staticPropertyAccess()
{
return self::$foo;
}

public function newInstanceCreation()
{
return new \ClassWithSelfNoNamespace;
}

public function catchSection()
{
try {
throw new \ClassWithSelfNoNamespace;
} catch (\ClassWithSelfNoNamespace $exception) {
// Nop
}
}

public function instanceCheck()
{
if ($this instanceof \ClassWithSelfNoNamespace) {
// Nop
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/** @noinspection PhpIllegalPsrClassPathInspection */
declare(strict_types=1);

class ClassWithSelfNoNamespace extends \Exception
{
const CLASS_CONST = self::class;

private static $foo = 42;

private self $instance;

public function acceptsAndReturnsSelf(self $instance): self
{
return $instance;
}

public function containsClosureWithSelf()
{
$func = function (self $instance): self {
return $instance;
};
$func($this);
}

public function staticMethodCall()
{
return self::staticPropertyAccess();
}

public function classConstantFetch()
{
return self::class . self::CLASS_CONST;
}

public static function staticPropertyAccess()
{
return self::$foo;
}

public function newInstanceCreation()
{
return new self;
}

public function catchSection()
{
try {
throw new self;
} catch (self $exception) {
// Nop
}
}

public function instanceCheck()
{
if ($this instanceof self) {
// Nop
}
}
}
1 change: 1 addition & 0 deletions tests/Go/Proxy/ClassProxyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function dataGenerator(): array
[First::class, 'publicMethod'],
[First::class, 'protectedMethod'],
[First::class, 'passByReference'],
[\ClassWithoutNamespace::class, 'publicMethod'],
];
}
}
11 changes: 11 additions & 0 deletions tests/Go/Stubs/ClassWithoutNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php /** @noinspection PhpIllegalPsrClassPathInspection */


class ClassWithoutNamespace
{
public function publicMethod(): int
{
return 1;
}

}

0 comments on commit 19f65c7

Please sign in to comment.