Completed
Push — master ( 331c43...fab006 )
by Miloš
01:13
created

ClassResolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Laganica\Di\Resolver;
4
5
use Laganica\Di\Definition\ClassDefinition;
6
use Laganica\Di\Definition\DefinitionInterface;
7
use Laganica\Di\Exception\NotFoundException;
8
9
/**
10
 * Class AutowireResolver
11
 *
12
 * @package Laganica\Di\Resolver
13
 */
14
class ClassResolver extends ReflectionResolver
15
{
16
    /**
17
     * @inheritDoc
18
     */
19 6
    public function resolve(DefinitionInterface $definition)
20
    {
21 6
        $this->validate($definition, ClassDefinition::class);
22
23 6
        $class = $definition->getClass();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Laganica\Di\Definition\DefinitionInterface as the method getClass() does only exist in the following implementations of said interface: Laganica\Di\Definition\ClassDefinition.

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...
24
25 6
        if (!class_exists($class)) {
26
            NotFoundException::create($class);
27
        }
28
29 6
        $params = $this->getConstructorParams($class);
30
31 6
        return new $class(...$params);
32
    }
33
}
34