Completed
Push — master ( 1cadcf...664b05 )
by Márk
06:08
created

PuliBetaStrategy::getCandidates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
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
 * @final
14
 *
15
 * @author David de Boer <[email protected]>
16
 * @author Márk Sági-Kazár <[email protected]>
17
 */
18
class PuliBetaStrategy implements DiscoveryStrategy
19
{
20
    /**
21
     * @var GeneratedPuliFactory
22
     */
23
    protected static $puliFactory;
24
25
    /**
26
     * @var Discovery
27
     */
28
    protected static $puliDiscovery;
29
30
    /**
31
     * @return GeneratedPuliFactory
32
     *
33
     * @throws PuliUnavailableException
34
     */
35
    private static function getPuliFactory()
36
    {
37
        if (null === self::$puliFactory) {
38
            if (!defined('PULI_FACTORY_CLASS')) {
39
                throw new PuliUnavailableException('Puli Factory is not available');
40
            }
41
42
            $puliFactoryClass = PULI_FACTORY_CLASS;
43
44
            if (!class_exists($puliFactoryClass)) {
45
                throw new PuliUnavailableException('Puli Factory class does not exist');
46
            }
47
48
            self::$puliFactory = new $puliFactoryClass();
49
        }
50
51
        return self::$puliFactory;
52
    }
53
54
    /**
55
     * Returns the Puli discovery layer.
56
     *
57
     * @return Discovery
58
     *
59
     * @throws PuliUnavailableException
60
     */
61
    private static function getPuliDiscovery()
62
    {
63
        if (!isset(self::$puliDiscovery)) {
64
            $factory = self::getPuliFactory();
65
            $repository = $factory->createRepository();
66
67
            self::$puliDiscovery = $factory->createDiscovery($repository);
68
        }
69
70
        return self::$puliDiscovery;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public static function getCandidates($type)
77
    {
78
        $returnData = [];
79
        $bindings = self::getPuliDiscovery()->findBindings($type);
80
81
        foreach ($bindings as $binding) {
82
            $condition = true;
83
            if ($binding->hasParameterValue('depends')) {
84
                $condition = $binding->getParameterValue('depends');
85
            }
86
            $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...
87
        }
88
89
        return $returnData;
90
    }
91
}
92