CommonPointCut::setInterceptor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2015-2020 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelAspect\PointCut;
21
22
use Ray\Aop\Matcher;
23
use Ray\Aop\Pointcut;
24
use Ray\Aop\MethodInterceptor;
25
26
/**
27
 * Class CommonPointCut
28
 */
29
class CommonPointCut
30
{
31
    /** @var MethodInterceptor */
32
    protected $interceptor;
33
34
    /** @var string */
35
    protected $annotation;
36
37
    /**
38
     * @param MethodInterceptor $interceptor
39
     */
40
    protected function setInterceptor(MethodInterceptor $interceptor): void
41
    {
42
        $this->interceptor = $interceptor;
43
    }
44
45
    /**
46
     * @return Pointcut
47
     */
48
    protected function withAnnotatedAnyInterceptor(): PointCut
49
    {
50
        if (method_exists($this->interceptor, 'setAnnotation')) {
51
            $this->interceptor->setAnnotation($this->annotation);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Ray\Aop\MethodInterceptor as the method setAnnotation() does only exist in the following implementations of said interface: Ytake\LaravelAspect\Interceptor\AbstractCache, Ytake\LaravelAspect\Inte...r\CacheEvictInterceptor, Ytake\LaravelAspect\Inte...tor\CachePutInterceptor, Ytake\LaravelAspect\Inte...or\CacheableInterceptor, Ytake\LaravelAspect\Inte...ogExceptionsInterceptor, Ytake\LaravelAspect\Inte...tor\LoggableInterceptor, Ytake\LaravelAspect\Inte...essageDrivenInterceptor, Ytake\LaravelAspect\Inte...tor\QueryLogInterceptor, Ytake\LaravelAspect\Inte...tryOnFailureInterceptor, Ytake\LaravelAspect\Inte...ransactionalInterceptor.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
52
        }
53
54
        return new Pointcut(
55
            (new Matcher)->any(),
56
            (new Matcher)->annotatedWith($this->annotation),
57
            [$this->interceptor]
58
        );
59
    }
60
}
61