Completed
Pull Request — master (#59)
by Tobias
10:40
created

Puli::resetPuliFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Http\Discovery\Strategy;
4
5
use Http\Discovery\Exception\PuliUnavailableException;
6
use Puli\Discovery\Api\Discovery;
7
8
/**
9
 * @author David de Boer <[email protected]>
10
 * @author Márk Sági-Kazár <[email protected]>
11
 */
12
class Puli implements DiscoveryStrategy
13
{
14
    /**
15
     * @var GeneratedPuliFactory
16
     */
17
    private static $puliFactory;
18
19
    /**
20
     * @var Discovery
21
     */
22
    private static $puliDiscovery;
23
24
    /**
25
     * @return GeneratedPuliFactory
26
     * 
27
     * @throws PuliUnavailableException
28
     */
29
    public static function getPuliFactory()
30
    {
31
        if (null === self::$puliFactory) {
32
            if (!defined('PULI_FACTORY_CLASS')) {
33
                throw new PuliUnavailableException('Puli Factory is not available');
34
            }
35
36
            $puliFactoryClass = PULI_FACTORY_CLASS;
37
38
            if (!class_exists($puliFactoryClass)) {
39
                throw new PuliUnavailableException('Puli Factory class does not exist');
40
            }
41
42
            self::$puliFactory = new $puliFactoryClass();
43
        }
44
45
        return self::$puliFactory;
46
    }
47
48
    /**
49
     * Sets the Puli factory.
50
     *
51
     * @param object $puliFactory
52
     */
53
    public static function setPuliFactory($puliFactory)
54
    {
55
        if (!is_callable([$puliFactory, 'createRepository']) || !is_callable([$puliFactory, 'createDiscovery'])) {
56
            throw new \InvalidArgumentException('The Puli Factory must expose a repository and a discovery');
57
        }
58
59
        self::$puliFactory = $puliFactory;
60
        self::$puliDiscovery = null;
61
    }
62
63
    /**
64
     * Resets the factory.
65
     */
66
    public static function resetPuliFactory()
67
    {
68
        self::$puliFactory = null;
69
        self::$puliDiscovery = null;
70
    }
71
72
    /**
73
     * Returns the Puli discovery layer.
74
     *
75
     * @return Discovery
76
     *
77
     * @throws PuliUnavailableException
78
     */
79
    public static function getPuliDiscovery()
80
    {
81
        if (!isset(self::$puliDiscovery)) {
82
            $factory = self::getPuliFactory();
83
            $repository = $factory->createRepository();
84
85
            self::$puliDiscovery = $factory->createDiscovery($repository);
86
        }
87
88
        return self::$puliDiscovery;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public static function find($type)
95
    {
96
        $returnData = [];
97
        $bindings = self::getPuliDiscovery()->findBindings($type);
98
99
        foreach ($bindings as $binding) {
100
            $condition = true;
101
            if ($binding->hasParameterValue('depends')) {
102
                $condition = $binding->getParameterValue('depends');
103
            }
104
            $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...
105
        }
106
107
        return $returnData;
108
    }
109
}
110