ServicesManager::replace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Onoi\CallbackContainer;
4
5
use Onoi\CallbackContainer\Exception\ServiceNotFoundException;
6
7
/**
8
 * Convenience class to handle services isolated from an active ContainerBuilder
9
 * instance.
10
 *
11
 * @license GNU GPL v2+
12
 * @since 2.0
13
 *
14
 * @author mwjames
15
 */
16
class ServicesManager {
17
18
	/**
19
	 * @var ContainerBuilder
20
	 */
21
	private $containerBuilder;
22
23
	/**
24
	 * @since 2.0
25
	 *
26
	 * @param ContainerBuilder $containerBuilder
27
	 */
28 7
	public function __construct( ContainerBuilder $containerBuilder ) {
29 7
		$this->containerBuilder = $containerBuilder;
30 7
	}
31
32
	/**
33
	 * @since 2.0
34
	 *
35
	 * @param string $serviceName
36
	 * @param mixed $service
37
	 * @param string|null $expectedReturnType
38
	 */
39 5
	public function add( $serviceName, $service, $expectedReturnType = null ) {
40
41 5
		if ( !is_callable( $service ) ) {
42 5
			$service = function() use( $service ) {
43 2
				return $service;
44 5
			};
45 5
		}
46
47 5
		$this->containerBuilder->registerCallback( $serviceName, $service );
48
49 5
		if ( $expectedReturnType !== null ) {
50 1
			$this->containerBuilder->registerExpectedReturnType( $serviceName, $expectedReturnType );
51 1
		}
52 5
	}
53
54
	/**
55
	 * @since 2.0
56
	 *
57
	 * @param string $serviceName
58
	 *
59
	 * @return boolean
60
	 */
61 5
	public function has( $serviceName ) {
62 5
		return $this->containerBuilder->isRegistered( $serviceName );
63
	}
64
65
	/**
66
	 * @since 2.0
67
	 *
68
	 * @param string $serviceName
69
	 *
70
	 * @return mixed
71
	 * @throws ServiceNotFoundException
72
	 */
73 5
	public function get( $serviceName ) {
74
75 5
		if ( !$this->containerBuilder->isRegistered( $serviceName ) ) {
76 1
			throw new ServiceNotFoundException( "$serviceName is an unknown service." );
77
		}
78
79 4
		$parameters = func_get_args();
80 4
		array_unshift( $parameters, $serviceName );
81
82 4
		return call_user_func_array( array( $this->containerBuilder, 'create' ), $parameters );
83
	}
84
85
	/**
86
	 * @since 2.0
87
	 *
88
	 * @param string $serviceName
89
	 */
90 1
	public function remove( $serviceName ) {
91 1
		$this->containerBuilder->deregister( $serviceName );
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Onoi\CallbackContainer\ContainerBuilder as the method deregister() does only exist in the following implementations of said interface: Onoi\CallbackContainer\CallbackContainerBuilder.

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...
92 1
	}
93
94
	/**
95
	 * @since 2.0
96
	 *
97
	 * @param string $serviceName
98
	 * @param mixed $service
99
	 */
100 2
	public function replace( $serviceName, $service ) {
101 2
		$this->containerBuilder->registerObject( $serviceName, $service );
102 2
	}
103
104
}
105