Completed
Push — master ( cb2703...d9d040 )
by Tobias
12:13
created

Transchoice::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[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 Translation\Bundle\Twig\Node;
13
14
class Transchoice extends \Twig_Node_Expression
15
{
16
    public function __construct(\Twig_Node_Expression_Array $arguments, $lineno)
17
    {
18
        parent::__construct(['arguments' => $arguments], [], $lineno);
19
    }
20
21
    public function compile(\Twig_Compiler $compiler)
22
    {
23
        $compiler->raw(
24
            sprintf(
25
                '$this->env->getExtension(\'%s\')->%s(',
26
                'Translation\Bundle\Twig\TranslationExtension',
27
                'transchoiceWithDefault'
28
            )
29
        );
30
31
        $first = true;
32
        foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Twig_Node as the method getKeyValuePairs() does only exist in the following sub-classes of Twig_Node: Twig\Node\Expression\ArrayExpression, Twig_Node_Expression_Array. 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...
33
            if (!$first) {
34
                $compiler->raw(', ');
35
            }
36
            $first = false;
37
38
            $compiler->subcompile($pair['value']);
39
        }
40
41
        $compiler->raw(')');
42
    }
43
}
44