PuliBetaStrategy   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 74
ccs 18
cts 24
cp 0.75
rs 10
c 0
b 0
f 0

3 Methods

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