Passed
Push — develop ( 91c1d2...446210 )
by Schlaefer
41s
created

ServiceLocator::getService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * @author  PhileCMS
4
 * @link    https://philecms.com
5
 * @license http://opensource.org/licenses/MIT
6
 */
7
8
namespace Phile\Core;
9
10
use Phile\Core\Container;
11
use Phile\Exception\ServiceLocatorException;
12
13
/**
14
 * the Service Locator class
15
 */
16
class ServiceLocator
17
{
18
    /**
19
     * method to register a service
20
     *
21
     * @param string $serviceKey the key for the service
22
     * @param mixed  $object
23
     *
24
     * @throws ServiceLocatorException
25
     */
26 26
    public static function registerService($serviceKey, $object)
27
    {
28 26
        Container::getInstance()->set($serviceKey, $object);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Container\ContainerInterface as the method set() does only exist in the following implementations of said interface: Phile\Core\Container.

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...
29 26
    }
30
31
    /**
32
     * checks if a service is registered
33
     *
34
     * @param string $serviceKey
35
     *
36
     * @return bool
37
     */
38 31
    public static function hasService($serviceKey)
39
    {
40 31
        return Container::getInstance()->has($serviceKey);
41
    }
42
43
    /**
44
     * returns a service
45
     *
46
     * @param string $serviceKey the service key
47
     *
48
     * @return mixed
49
     * @throws ServiceLocatorException
50
     */
51 29
    public static function getService($serviceKey)
52
    {
53 29
        return Container::getInstance()->get($serviceKey);
54
    }
55
}
56