FactoryResolver::resolve()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 4
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Container package.
5
 *
6
 * Copyright (c) Miloš Đurić <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Laganica\Di\Resolver;
13
14
use Laganica\Di\Definition\DefinitionInterface;
15
use Laganica\Di\Definition\FactoryDefinition;
16
use Laganica\Di\Exception\ClassNotFoundException;
17
use Laganica\Di\Exception\InvalidFactoryException;
18
use Laganica\Di\FactoryInterface;
19
20
/**
21
 * Class FactoryResolver
22
 *
23
 * @package Laganica\Di\Resolver
24
 */
25
class FactoryResolver extends Resolver
26
{
27
    /**
28
     * @inheritDoc
29
     */
30 5
    public function resolve(DefinitionInterface $definition)
31
    {
32 5
        $this->validate($definition, FactoryDefinition::class);
33
34 5
        $factoryClass = $definition->getFactoryClass();
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 getFactoryClass() does only exist in the following implementations of said interface: Laganica\Di\Definition\FactoryDefinition.

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...
35
36 5
        if (!class_exists($factoryClass)) {
37 1
            throw ClassNotFoundException::create($factoryClass);
38
        }
39
40 4
        $factoryInterface = FactoryInterface::class;
41 4
        $interfaces = class_implements($factoryClass);
42
43 4
        if (empty($interfaces) || !in_array($factoryInterface, $interfaces, true)) {
44 1
            throw InvalidFactoryException::create($factoryClass, $factoryInterface);
45
        }
46
47 3
        return (new $factoryClass)($this->getContainer());
48
    }
49
}
50