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