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/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]); } 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();