Neos\Flow\Aop\Pointcut\PointcutMethodNameFilter::matches PHP Method

matches() public method

Returns TRUE if method name, visibility and arguments constraints match and the target method is not final.
public matches ( string $className, string $methodName, string $methodDeclaringClassName, mixed $pointcutQueryIdentifier ) : boolean
$className string Ignored in this pointcut filter
$methodName string Name of the method to match against
$methodDeclaringClassName string Name of the class the method was originally declared in
$pointcutQueryIdentifier mixed Some identifier for this query - must at least differ from a previous identifier. Used for circular reference detection.
return boolean TRUE if the class matches, otherwise FALSE
    public function matches($className, $methodName, $methodDeclaringClassName, $pointcutQueryIdentifier)
    {
        $matchResult = preg_match('/^' . $this->methodNameFilterExpression . '$/', $methodName);
        if ($matchResult === false) {
            throw new Exception('Error in regular expression', 1168876915);
        } elseif ($matchResult !== 1) {
            return false;
        }
        switch ($this->methodVisibility) {
            case 'public':
                if (!($methodDeclaringClassName !== null && $this->reflectionService->isMethodPublic($methodDeclaringClassName, $methodName))) {
                    return false;
                }
                break;
            case 'protected':
                if (!($methodDeclaringClassName !== null && $this->reflectionService->isMethodProtected($methodDeclaringClassName, $methodName))) {
                    return false;
                }
                break;
        }
        if ($methodDeclaringClassName !== null && $this->reflectionService->isMethodFinal($methodDeclaringClassName, $methodName)) {
            return false;
        }
        $methodArguments = $methodDeclaringClassName === null ? [] : $this->reflectionService->getMethodParameters($methodDeclaringClassName, $methodName);
        foreach (array_keys($this->methodArgumentConstraints) as $argumentName) {
            $objectAccess = explode('.', $argumentName, 2);
            $argumentName = $objectAccess[0];
            if (!array_key_exists($argumentName, $methodArguments)) {
                $this->systemLogger->log('The argument "' . $argumentName . '" declared in pointcut does not exist in method ' . $methodDeclaringClassName . '->' . $methodName, LOG_NOTICE);
                return false;
            }
        }
        return true;
    }

Usage Example

    /**
     * @test
     */
    public function matchesChecksTheAvailablityOfAnArgumentNameIfArgumentConstraintsHaveBeenConfigured()
    {
        $className = 'TestClass' . md5(uniqid(mt_rand(), true));
        eval('
			class ' . $className . " {\n\t\t\t\tpublic function somePublicMethod(\$arg1) {}\n\t\t\t\tpublic function someOtherPublicMethod(\$arg1, \$arg2 = 'default') {}\n\t\t\t\tpublic function someThirdMethod(\$arg1, \$arg2, \$arg3 = 'default') {}\n\t\t\t}");
        $mockReflectionService = $this->createMock(ReflectionService::class);
        $mockReflectionService->expects($this->exactly(3))->method('getMethodParameters')->will($this->onConsecutiveCalls(['arg1' => []], ['arg1' => [], 'arg2' => []], ['arg1' => [], 'arg2' => [], 'arg3' => []]));
        $mockSystemLogger = $this->getMockBuilder(\Neos\Flow\Log\Logger::class)->setMethods(['log'])->getMock();
        $mockSystemLogger->expects($this->once())->method('log')->with($this->equalTo('The argument "arg2" declared in pointcut does not exist in method ' . $className . '->somePublicMethod'));
        $argumentConstraints = ['arg1' => ['operator' => '==', 'value' => 'someValue'], 'arg2.some.sub.object' => ['operator' => '==', 'value' => 'someValue']];
        $methodNameFilter = new Aop\Pointcut\PointcutMethodNameFilter('some.*', null, $argumentConstraints);
        $methodNameFilter->injectReflectionService($mockReflectionService);
        $methodNameFilter->injectSystemLogger($mockSystemLogger);
        $methodNameFilter->matches(__CLASS__, 'somePublicMethod', $className, 1);
        $this->assertTrue($methodNameFilter->matches(__CLASS__, 'someOtherPublicMethod', $className, 1));
        $this->assertTrue($methodNameFilter->matches(__CLASS__, 'someThirdMethod', $className, 1));
    }