Completed
Push — master ( 17f23a...c8aac9 )
by Konstantin
02:44 queued 44s
created

MagicCallPatch::apply()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 26
rs 8.5806
cc 4
eloc 15
nc 6
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Doubler\ClassPatch;
13
14
use phpDocumentor\Reflection\DocBlock;
15
use Prophecy\Doubler\Generator\Node\ClassNode;
16
use Prophecy\Doubler\Generator\Node\MethodNode;
17
18
/**
19
 * Discover Magical API using "@method" PHPDoc format.
20
 *
21
 * @author Thomas Tourlourat <[email protected]>
22
 */
23
class MagicCallPatch implements ClassPatchInterface
24
{
25
    /**
26
     * Support any class
27
     *
28
     * @param ClassNode $node
29
     *
30
     * @return boolean
31
     */
32
    public function supports(ClassNode $node)
33
    {
34
        return true;
35
    }
36
37
    /**
38
     * Discover Magical API
39
     *
40
     * @param ClassNode $node
41
     */
42
    public function apply(ClassNode $node)
43
    {
44
        $parentClass = $node->getParentClass();
45
        $reflectionClass = new \ReflectionClass($parentClass);
46
47
        $phpdoc = new DocBlock($reflectionClass->getDocComment());
48
49
        $tagList = $phpdoc->getTagsByName('method');
50
51
        $interfaces = $reflectionClass->getInterfaces();
52
        foreach($interfaces as $interface) {
53
            $phpdoc = new DocBlock($interface);
54
            $tagList = array_merge($tagList, $phpdoc->getTagsByName('method'));
55
        }
56
57
        foreach($tagList as $tag) {
58
            $methodName = $tag->getMethodName();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class phpDocumentor\Reflection\DocBlock\Tag as the method getMethodName() does only exist in the following sub-classes of phpDocumentor\Reflection\DocBlock\Tag: phpDocumentor\Reflection\DocBlock\Tag\MethodTag. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
59
60
            if (!$reflectionClass->hasMethod($methodName)) {
61
                $methodNode = new MethodNode($tag->getMethodName());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class phpDocumentor\Reflection\DocBlock\Tag as the method getMethodName() does only exist in the following sub-classes of phpDocumentor\Reflection\DocBlock\Tag: phpDocumentor\Reflection\DocBlock\Tag\MethodTag. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
62
                $methodNode->setStatic($tag->isStatic());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class phpDocumentor\Reflection\DocBlock\Tag as the method isStatic() does only exist in the following sub-classes of phpDocumentor\Reflection\DocBlock\Tag: phpDocumentor\Reflection\DocBlock\Tag\MethodTag. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
63
64
                $node->addMethod($methodNode);
65
            }
66
        }
67
    }
68
69
    /**
70
     * Returns patch priority, which determines when patch will be applied.
71
     *
72
     * @return integer Priority number (higher - earlier)
73
     */
74
    public function getPriority()
75
    {
76
        return 50;
77
    }
78
}
79
80