Completed
Pull Request — master (#59)
by Tobias
07:03
created

Puli::getPuliFactory()   A

Complexity

Conditions 4
Paths 4

Size

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