From 3790ed75ea15f8ceff2e5f52de56f361969502a3 Mon Sep 17 00:00:00 2001 From: WalterWoshid Date: Fri, 13 Nov 2026 23:25:53 +0100 Subject: [PATCH 1/3] fix: replace native assertions with explicit checks --- src/Core/Transform/ProxiedClassModifier.php | 9 +++++++-- tests/ClassLoaderMockTrait.php | 4 ++-- .../Aspect/CalculatorLoggerAspect.php | 11 +++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Core/Transform/ProxiedClassModifier.php b/src/Core/Transform/ProxiedClassModifier.php index 406f91d..665475d 100644 --- a/src/Core/Transform/ProxiedClassModifier.php +++ b/src/Core/Transform/ProxiedClassModifier.php @@ -4,6 +4,7 @@ namespace Okapi\Aop\Core\Transform; use DI\Attribute\Inject; +use LogicException; use Microsoft\PhpParser\Node; use Microsoft\PhpParser\Token; use Microsoft\PhpParser\TokenKind; @@ -142,7 +143,9 @@ private function convertToProxy(): void { // Find the class declaration $node = $this->sourceFileNode->getFirstDescendantNode(Node\Statement\ClassDeclaration::class); - assert($node instanceof Node\Statement\ClassDeclaration, 'The transformed source must declare a class.'); + if (!$node instanceof Node\Statement\ClassDeclaration) { + throw new LogicException('The transformed source must declare a class.'); + } // Replace the class name $this->edit($node->name, $this->proxiedClassName); @@ -312,7 +315,9 @@ private function replaceMagicConstants(): void if (!$methodNode) { break; } - assert($methodNode instanceof Node\MethodDeclaration, 'The enclosing node must be a method.'); + if (!$methodNode instanceof Node\MethodDeclaration) { + throw new LogicException('The enclosing node must be a method.'); + } $originalNamespacedClassName = $this->code->getNamespacedClass(); $originalMethodName = $methodNode->getName(); diff --git a/tests/ClassLoaderMockTrait.php b/tests/ClassLoaderMockTrait.php index 8f18910..5af88b3 100644 --- a/tests/ClassLoaderMockTrait.php +++ b/tests/ClassLoaderMockTrait.php @@ -20,7 +20,7 @@ private function findClassMock(string $class): string $this->findClassLoader(); } - assert($this->classLoader instanceof ClassLoader, 'The invocation must match the configured test fixture.'); + Assert::assertInstanceOf(ClassLoader::class, $this->classLoader); /** @var mixed $file */ $file = $this->classLoader->findFile($class); Assert::assertIsString($file); @@ -33,7 +33,7 @@ private function findOriginalClassMock(string $class): string $this->findClassLoader(); } - assert($this->classLoader instanceof ClassLoader, 'The invocation must match the configured test fixture.'); + Assert::assertInstanceOf(ClassLoader::class, $this->classLoader); $original = new ReflectionProperty(ClassLoader::class, 'originalClassLoader'); /** @var mixed $loader */ $loader = $original->getValue($this->classLoader); diff --git a/tests/Functional/AdviceBehavior/BeforeAroundAfterAdviceOnSameAdviceMethod/Aspect/CalculatorLoggerAspect.php b/tests/Functional/AdviceBehavior/BeforeAroundAfterAdviceOnSameAdviceMethod/Aspect/CalculatorLoggerAspect.php index c411751..789d793 100644 --- a/tests/Functional/AdviceBehavior/BeforeAroundAfterAdviceOnSameAdviceMethod/Aspect/CalculatorLoggerAspect.php +++ b/tests/Functional/AdviceBehavior/BeforeAroundAfterAdviceOnSameAdviceMethod/Aspect/CalculatorLoggerAspect.php @@ -12,6 +12,7 @@ use Okapi\Aop\Invocation\MethodInvocation; use Okapi\Aop\Tests\Functional\AdviceBehavior\BeforeAroundAfterAdviceOnSameAdviceMethod\Target\Calculator; use Okapi\Aop\Tests\Stubs\Etc\Logger; +use PHPUnit\Framework\Assert; #[Aspect] class CalculatorLoggerAspect @@ -30,8 +31,9 @@ public function logCalculation(MethodInvocation $invocation): void } if ($adviceType === AdviceType::Around) { - assert( - $invocation instanceof AroundMethodInvocation, + Assert::assertInstanceOf( + AroundMethodInvocation::class, + $invocation, 'The invocation must match the configured test fixture.', ); @@ -45,8 +47,9 @@ public function logCalculation(MethodInvocation $invocation): void } if ($adviceType === AdviceType::After) { - assert( - $invocation instanceof \Okapi\Aop\Invocation\AfterMethodInvocation, + Assert::assertInstanceOf( + \Okapi\Aop\Invocation\AfterMethodInvocation::class, + $invocation, 'The invocation must match the configured test fixture.', ); /** @var int $result */ From 505e6f668fc159d0e0a33e615d7c2a0c37810806 Mon Sep 17 00:00:00 2001 From: WalterWoshid Date: Fri, 13 Nov 2026 23:31:39 +0100 Subject: [PATCH 2/3] chore: forbid native assertions in Mago --- mago-tests.toml | 5 +++++ mago.toml | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/mago-tests.toml b/mago-tests.toml index f4b7ad0..9c70342 100644 --- a/mago-tests.toml +++ b/mago-tests.toml @@ -9,6 +9,11 @@ excludes = ["tests/cache", "tests/coverage"] [linter] integrations = ["phpunit"] +[linter.rules.disallowed-functions] +enabled = true +level = "error" +functions = ["assert"] + # Preserve the library's coercion contract and existing public call signatures. [linter.rules.strict-types] enabled = false diff --git a/mago.toml b/mago.toml index 6744327..a4bdb66 100644 --- a/mago.toml +++ b/mago.toml @@ -9,6 +9,11 @@ excludes = ["tests/cache", "tests/coverage"] [linter] integrations = ["phpunit"] +[linter.rules.disallowed-functions] +enabled = true +level = "error" +functions = ["assert"] + # Preserve the library's coercion contract and existing public call signatures. [linter.rules.strict-types] enabled = false From a240821d043efaa025f8331cf6837f0e2fe92e55 Mon Sep 17 00:00:00 2001 From: WalterWoshid Date: Fri, 13 Nov 2026 23:43:17 +0100 Subject: [PATCH 3/3] chore(deps): update code-transformer to 1.3.8 --- composer.json | 2 +- src/AopKernel.php | 2 +- src/Core/AutoloadInterceptor/ClassLoader.php | 10 +++------- tests/Integration/TransformerAndAspect/Kernel.php | 2 +- .../Kernel.php | 2 +- 5 files changed, 7 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 88c443d..16a84f4 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "require": { "php": ">=8.1", "nette/php-generator": "^4.0", - "okapi/code-transformer": "1.3.7", + "okapi/code-transformer": "1.3.8", "okapi/wildcards": "^1.0", "okapi/singleton": "^1.0", "php-di/php-di": "^7.0" diff --git a/src/AopKernel.php b/src/AopKernel.php index ff7fe55..855957f 100644 --- a/src/AopKernel.php +++ b/src/AopKernel.php @@ -67,7 +67,7 @@ abstract class AopKernel extends CodeTransformerKernel /** * The exclude paths. Paths/directories in this array will be excluded * - * @var array + * @var string[] */ protected array $excludePaths = []; diff --git a/src/Core/AutoloadInterceptor/ClassLoader.php b/src/Core/AutoloadInterceptor/ClassLoader.php index 4c42fc5..16d0543 100644 --- a/src/Core/AutoloadInterceptor/ClassLoader.php +++ b/src/Core/AutoloadInterceptor/ClassLoader.php @@ -34,16 +34,13 @@ class ClassLoader extends CodeTransformerClassLoader /** * Find the path to the file and match and apply the aspects. * - * @param class-string $namespacedClass + * @param string $class * * @return false|string - * - * @noinspection PhpStatementHasEmptyBodyInspection */ - // CodeTransformer uses $namespacedClass; its Composer parent uses $class. - // Keep our immediate parent's existing named-argument contract until that dependency is aligned. - public function findFile($namespacedClass): false|string + public function findFile($class): false|string { + $namespacedClass = $class; $filePath = $this->originalClassLoader->findFile($namespacedClass); // @codeCoverageIgnoreStart @@ -63,7 +60,6 @@ public function findFile($namespacedClass): false|string return false; } - /** @var string[] $excludePaths */ $excludePaths = $this->options->getExcludePaths(); foreach ($excludePaths as $path) { $resolvedPath = Path::resolve($path); diff --git a/tests/Integration/TransformerAndAspect/Kernel.php b/tests/Integration/TransformerAndAspect/Kernel.php index fe36e83..5e2d9a0 100644 --- a/tests/Integration/TransformerAndAspect/Kernel.php +++ b/tests/Integration/TransformerAndAspect/Kernel.php @@ -11,7 +11,7 @@ class Kernel extends AopKernel { protected ?string $cacheDir = Util::CACHE_DIR; - /** @var array> */ + /** @var array */ protected array $transformers = [ FixIncorrectFunctionTransformer::class, ]; diff --git a/tests/Integration/TransformerAndAspectDependencyInjectionHandler/Kernel.php b/tests/Integration/TransformerAndAspectDependencyInjectionHandler/Kernel.php index 65c0ec0..3822d2f 100644 --- a/tests/Integration/TransformerAndAspectDependencyInjectionHandler/Kernel.php +++ b/tests/Integration/TransformerAndAspectDependencyInjectionHandler/Kernel.php @@ -31,7 +31,7 @@ protected function dependencyInjectionHandler(): ?Closure Aspect::class, ]; - /** @var array> */ + /** @var array */ protected array $transformers = [ Transformer::class, ];