Completed
Pull Request — master (#201)
by
unknown
02:17
created

JsFunctionsScanner::getArgumentValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gettext\Utils;
4
5
use Peast\Peast;
6
use Peast\Syntax\Node\CallExpression;
7
use Peast\Syntax\Node\MemberExpression;
8
use Peast\Syntax\Node\Node;
9
use Peast\Traverser;
10
11
class JsFunctionsScanner extends FunctionsScanner
12
{
13
    private $ast;
14
15
    /**
16
     * Constructor.
17
     *
18
     * @param string $code The php code to scan
19
     */
20
    public function __construct($code)
21
    {
22
        $this->ast = Peast::latest($code, [
23
            'sourceType' => Peast::SOURCE_TYPE_MODULE,
24
            'jsx' => true,
25
        ])->parse();
26
    }
27
28
    public function getFunctions(array $constants = [])
29
    {
30
        $functions = [];
31
32
        $traverser = new Traverser;
33
        $traverser->addFunction(function ($node) use (&$functions) {
34
            if (!$this->isCallExpression($node)) {
35
                return;
36
            }
37
38
            $functions[] = [
39
                $this->getNodeName($node),
40
                $this->getStartingLineNumber($node),
41
                $this->getArguments($node),
42
            ];
43
        });
44
45
        $traverser->traverse($this->ast);
46
47
        return $functions;
48
    }
49
50
    /**
51
     * Checks if given $node is a CallExpression.
52
     *
53
     * @param Node $node
54
     *
55
     * @return bool
56
     */
57
    private function isCallExpression(Node $node)
58
    {
59
        return $node instanceof CallExpression || $node->getType() === 'CallExpression';
60
    }
61
62
    /**
63
     * Returns given $node's name.
64
     *
65
     * @param Node $node
66
     *
67
     * @return string|null
68
     */
69
    private function getNodeName(Node $node)
70
    {
71
        if (method_exists($node, 'getCallee')) {
72
            return $this->getNodeName($node->getCallee());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Peast\Syntax\Node\Node as the method getCallee() does only exist in the following sub-classes of Peast\Syntax\Node\Node: Peast\Syntax\Node\CallExpression, Peast\Syntax\Node\NewExpression. 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...
73
        }
74
75
        if ($node instanceof MemberExpression && $node->getProperty() instanceof Node) {
76
            return $this->getNodeName($node->getProperty());
77
        }
78
79
        if (method_exists($node, 'getName')) {
80
            return $node->getName();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Peast\Syntax\Node\Node as the method getName() does only exist in the following sub-classes of Peast\Syntax\Node\Node: Peast\Syntax\Node\Identifier, Peast\Syntax\Node\JSX\JSXAttribute, Peast\Syntax\Node\JSX\JSXBoundaryElement, Peast\Syntax\Node\JSX\JSXClosingElement, Peast\Syntax\Node\JSX\JSXIdentifier, Peast\Syntax\Node\JSX\JSXNamespacedName, Peast\Syntax\Node\JSX\JSXOpeningElement. 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...
81
        }
82
83
        return null;
84
    }
85
86
    /**
87
     * Returns the starting line number of given $node.
88
     *
89
     * @param Node $node
90
     *
91
     * @return int
92
     */
93
    private function getStartingLineNumber(Node $node)
94
    {
95
        return $node->getLocation()->getStart()->getLine();
96
    }
97
98
    /**
99
     * Returns an array of arguments for given $node.
100
     *
101
     * @param Node $node
102
     *
103
     * @return array
104
     */
105
    private function getArguments(Node $node)
106
    {
107
        return array_map(function($argument) {
108
            return $this->getArgumentValue($argument);
109
        }, $node->getArguments());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Peast\Syntax\Node\Node as the method getArguments() does only exist in the following sub-classes of Peast\Syntax\Node\Node: Peast\Syntax\Node\CallExpression, Peast\Syntax\Node\NewExpression. 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...
110
    }
111
112
    /**
113
     * Returns argument value of given $node.
114
     *
115
     * @param Node $node
116
     *
117
     * @return string|null
118
     */
119
    private function getArgumentValue(Node $node)
120
    {
121
        if ($node->getType() === 'Literal') {
122
            return $node->getValue();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Peast\Syntax\Node\Node as the method getValue() does only exist in the following sub-classes of Peast\Syntax\Node\Node: Peast\Syntax\Node\AssignmentProperty, Peast\Syntax\Node\BooleanLiteral, Peast\Syntax\Node\JSX\JSXAttribute, Peast\Syntax\Node\JSX\JSXText, Peast\Syntax\Node\Literal, Peast\Syntax\Node\MethodDefinition, Peast\Syntax\Node\NullLiteral, Peast\Syntax\Node\NumericLiteral, Peast\Syntax\Node\Property, Peast\Syntax\Node\RegExpLiteral, Peast\Syntax\Node\StringLiteral, Peast\Syntax\Node\TemplateElement. 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...
123
        }
124
125
        return null;
126
    }
127
}
128