From da68d5efbbe39ac8c3f34e60216ef42f8c6dd097 Mon Sep 17 00:00:00 2001 From: Gert de Pagter Date: Fri, 23 Jun 2023 11:32:44 +0200 Subject: [PATCH 1/2] Make sure `new` calls are also filtered by definedIn --- src/Calls/NewCalls.php | 4 +- tests/Calls/MethodCallsDefinedInTest.php | 4 +- tests/Calls/NewCallsDefinedInTest.php | 63 +++++++++++++++++++ tests/libs/TestException.php | 8 +++ tests/src/disallowed/methodCallsDefinedIn.php | 11 ++++ 5 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 tests/Calls/NewCallsDefinedInTest.php create mode 100644 tests/libs/TestException.php diff --git a/src/Calls/NewCalls.php b/src/Calls/NewCalls.php index 01d5480..c105a5e 100644 --- a/src/Calls/NewCalls.php +++ b/src/Calls/NewCalls.php @@ -88,10 +88,12 @@ public function processNode(Node $node, Scope $scope): array } foreach ($names as $name) { + $classRef = $type->getClassReflection(); + $definedIn = $classRef ? $classRef->getFileName() : null; $name .= self::CONSTRUCT; $errors = array_merge( $errors, - $this->disallowedCallsRuleErrors->get($node, $scope, $name, $type->getClassName() . self::CONSTRUCT, null, $this->disallowedCalls) + $this->disallowedCallsRuleErrors->get($node, $scope, $name, $type->getClassName() . self::CONSTRUCT, $definedIn, $this->disallowedCalls) ); } } diff --git a/tests/Calls/MethodCallsDefinedInTest.php b/tests/Calls/MethodCallsDefinedInTest.php index 2d2e8d5..0ea2f06 100644 --- a/tests/Calls/MethodCallsDefinedInTest.php +++ b/tests/Calls/MethodCallsDefinedInTest.php @@ -59,11 +59,11 @@ public function testRule(): void // expect this error message: 'Calling Waldo\Quux\Blade::andSorcery() is forbidden, because reasons [Waldo\Quux\Blade::andSorcery() matches *()]', // on this line: - 9, + 10, ], [ 'Calling Waldo\Quux\Blade::server() is forbidden, because reasons [Waldo\Quux\Blade::server() matches *()]', - 10, + 11, ], ]); // Based on the configuration above, no errors in this file: diff --git a/tests/Calls/NewCallsDefinedInTest.php b/tests/Calls/NewCallsDefinedInTest.php new file mode 100644 index 0000000..487f534 --- /dev/null +++ b/tests/Calls/NewCallsDefinedInTest.php @@ -0,0 +1,63 @@ + '*', + 'definedIn' => 'libs/Bl*', + 'allowIn' => [ + 'src/disallowed-allow/*.php', + 'src/*-allow/*.*', + ], + ], + ] + ); + } + + + public function testRule(): void + { + // Based on the configuration above, in this file: + $this->analyse([__DIR__ . '/../src/disallowed/methodCallsDefinedIn.php'], [ + [ + // expect this error message: + 'Calling Waldo\Quux\Blade::__construct() is forbidden, because reasons [Waldo\Quux\Blade::__construct() matches *()]', + // on this line: + 9, + ], + ]); + // Based on the configuration above, no errors in this file: + $this->analyse([__DIR__ . '/../src/disallowed-allow/methodCallsDefinedIn.php'], []); + } + +} diff --git a/tests/libs/TestException.php b/tests/libs/TestException.php new file mode 100644 index 0000000..87aed75 --- /dev/null +++ b/tests/libs/TestException.php @@ -0,0 +1,8 @@ +getMessage(); +$exception->getPrevious(); + +// allowed because it's defined elsewhere +$testException = new TestException(); +$testException->getMessage(); +$testException->getPrevious(); From 7da5e6606d98635d1cc6df34fc84a4dd2a593814 Mon Sep 17 00:00:00 2001 From: Gert de Pagter Date: Fri, 23 Jun 2023 11:32:44 +0200 Subject: [PATCH 2/2] Add return type to IteratorAggregate interface methods --- tests/libs/Option.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/libs/Option.php b/tests/libs/Option.php index dbbe8e8..5eca7b7 100644 --- a/tests/libs/Option.php +++ b/tests/libs/Option.php @@ -7,6 +7,7 @@ use EmptyIterator; use IteratorAggregate; use ArrayIterator; +use Traversable; /** * @template T @@ -62,7 +63,7 @@ public static function create() return self::$instance; } - public function getIterator() + public function getIterator(): Traversable { return new EmptyIterator(); } @@ -104,7 +105,7 @@ public static function create($value) } - public function getIterator() + public function getIterator(): Traversable { return new ArrayIterator([$this->value]); }