Completed
Push — master ( fac124...ff4c77 )
by Márk
10s
created

Puli::getPuliFactory()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 9.488

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.2
ccs 3
cts 10
cp 0.3
cc 4
eloc 9
nc 4
nop 0
crap 9.488
1
<?php
2
3
namespace Http\Discovery\Strategy;
4
5
use Http\Discovery\Exception\PuliUnavailableException;
6
use Puli\Discovery\Api\Discovery;
7
use Puli\GeneratedPuliFactory;
8
9
/**
10
 * Find candidates using Puli.
11
 *
12
 * @internal
13
 *
14
 * @author David de Boer <[email protected]>
15
 * @author Márk Sági-Kazár <[email protected]>
16
 */
17
class Puli implements DiscoveryStrategy
18
{
19
    /**
20
     * @var GeneratedPuliFactory
21
     */
22
    protected static $puliFactory;
23
24
    /**
25
     * @var Discovery
26
     */
27
    protected static $puliDiscovery;
28
29
    /**
30
     * @return GeneratedPuliFactory
31
     *
32
     * @throws PuliUnavailableException
33
     */
34 2
    private static function getPuliFactory()
35
    {
36 2
        if (null === self::$puliFactory) {
37
            if (!defined('PULI_FACTORY_CLASS')) {
38
                throw new PuliUnavailableException('Puli Factory is not available');
39
            }
40
41
            $puliFactoryClass = PULI_FACTORY_CLASS;
42
43
            if (!class_exists($puliFactoryClass)) {
44
                throw new PuliUnavailableException('Puli Factory class does not exist');
45
            }
46
47
            self::$puliFactory = new $puliFactoryClass();
48
        }
49
50 2
        return self::$puliFactory;
51
    }
52
53
    /**
54
     * Returns the Puli discovery layer.
55
     *
56
     * @return Discovery
57
     *
58
     * @throws PuliUnavailableException
59
     */
60 2
    private static function getPuliDiscovery()
61
    {
62 2
        if (!isset(self::$puliDiscovery)) {
63 2
            $factory = self::getPuliFactory();
64 2
            $repository = $factory->createRepository();
65
66 2
            self::$puliDiscovery = $factory->createDiscovery($repository);
67 2
        }
68
69 2
        return self::$puliDiscovery;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public static function getCandidates($type)
76
    {
77 2
        $returnData = [];
78 2
        $bindings = self::getPuliDiscovery()->findBindings($type);
79
80 2
        foreach ($bindings as $binding) {
81 2
            $condition = true;
82 2
            if ($binding->hasParameterValue('depends')) {
83 1
                $condition = $binding->getParameterValue('depends');
84 1
            }
85 2
            $returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition];
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Puli\Discovery\Api\Binding\Binding as the method getClassName() does only exist in the following implementations of said interface: Puli\Discovery\Binding\ClassBinding.

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...
86 2
        }
87
88 2
        return $returnData;
89
    }
90
}
91