Completed
Pull Request — master (#59)
by Tobias
11:53 queued 01:54
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
    public static function getPuliFactory()
28
    {
29
        if (null === self::$puliFactory) {
30
            if (!defined('PULI_FACTORY_CLASS')) {
31
                throw new PuliUnavailableException('Puli Factory is not available');
32
            }
33
34
            $puliFactoryClass = PULI_FACTORY_CLASS;
35
36
            if (!class_exists($puliFactoryClass)) {
37
                throw new PuliUnavailableException('Puli Factory class does not exist');
38
            }
39
40
            self::$puliFactory = new $puliFactoryClass();
41
        }
42
43
        return self::$puliFactory;
44
    }
45
46
    /**
47
     * Sets the Puli factory.
48
     *
49
     * @param object $puliFactory
50
     */
51
    public static function setPuliFactory($puliFactory)
52
    {
53
        if (!is_callable([$puliFactory, 'createRepository']) || !is_callable([$puliFactory, 'createDiscovery'])) {
54
            throw new \InvalidArgumentException('The Puli Factory must expose a repository and a discovery');
55
        }
56
57
        self::$puliFactory = $puliFactory;
58
        self::$puliDiscovery = null;
59
    }
60
61
    /**
62
     * Resets the factory.
63
     */
64
    public static function resetPuliFactory()
65
    {
66
        self::$puliFactory = null;
67
        self::$puliDiscovery = null;
68
    }
69
70
    /**
71
     * Returns the Puli discovery layer.
72
     *
73
     * @return Discovery
74
     */
75
    public static function getPuliDiscovery()
76
    {
77
        if (!isset(self::$puliDiscovery)) {
78
            $factory = self::getPuliFactory();
79
            $repository = $factory->createRepository();
80
81
            self::$puliDiscovery = $factory->createDiscovery($repository);
82
        }
83
84
        return self::$puliDiscovery;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public static function find($type)
91
    {
92
        $returnData = [];
93
        $bindings = self::getPuliDiscovery()->findBindings($type);
94
95
        foreach ($bindings as $binding) {
96
            $condition = true;
97
            if ($binding->hasParameterValue('depends')) {
98
                $condition = $binding->getParameterValue('depends');
99
            }
100
            $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...
101
        }
102
103
        return $returnData;
104
    }
105
}
106