Completed
Pull Request — master (#249)
by San
08:48
created

Loader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Alice\Fixtures;
4
5
use Knp\FriendlyContexts\Alice\ProviderResolver;
6
use Nelmio\Alice\Loader\NativeLoader as BaseLoader;
7
8
class Loader extends BaseLoader
9
{
10
    private $cache = [];
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
11
12
    public function __construct($locale, ProviderResolver $providers)
13
    {
14
        parent::__construct($locale, $providers->all());
0 ignored issues
show
Unused Code introduced by
The call to NativeLoader::__construct() has too many arguments starting with $providers->all().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
15
    }
16
17
    public function getCache()
18
    {
19
        return $this->cache;
20
    }
21
22
    public function clearCache()
23
    {
24
        $this->cache = [];
25
    }
26
27
    public function load($filename)
28
    {
29
        return $this->loadFile($filename);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function instantiateFixtures(array $fixtures)
36
    {
37
        parent::instantiateFixtures($fixtures);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Nelmio\Alice\Loader\NativeLoader as the method instantiateFixtures() does only exist in the following sub-classes of Nelmio\Alice\Loader\NativeLoader: Knp\FriendlyContexts\Alice\Fixtures\Loader. 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...
38
39
        foreach ($fixtures as $fixture) {
40
            $spec = array_map(function ($property) {
41
                return $property->getValue();
42
            }, $fixture->getProperties());
43
44
            $this->cache[] = [ $spec, $this->objects->get($fixture->getName()) ];
0 ignored issues
show
Bug introduced by
The property objects does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
        }
46
    }
47
}
48