Completed
Pull Request — master (#59)
by Tobias
24:59 queued 15:01
created

Puli   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 98
rs 10

5 Methods

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