ServiceCanHoldService2::getService2()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Example;
4
5
use SlidesWorker\ServiceLocator\ServiceLocator;
6
use SlidesWorker\ServiceLocator\ServiceLocatorInterface;
7
use SlidesWorker\ServiceLocator\ServiceLocatorAwareInterface;
8
use SlidesWorker\ServiceLocator\ServiceLocatorAwareTrait;
9
use SlidesWorker\ServiceLocator\Initializer\InitializerInterface;
10
11
class ServiceCanHoldService2 implements ServiceTwoAwareInterface
12
{
13
    protected $service2;
14
    public function setService2($service2)
15
    {
16
        $this->service2  = $service2;
17
    }
18
    public function getService2()
19
    {
20
        return $this->service2;
21
    }
22
}
23
24
interface ServiceTwoAwareInterface
25
{
26
    public function setService2($service2);
27
    public function getService2();
28
}
29
class Service2
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Example\Service2 has been defined more than once; this definition is ignored, only the first definition in example/SimpleWork.php (L11-13) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
30
{
31
    public function foo()
32
    {
33
    }
34
}
35
class Service2Initializer implements InitializerInterface
36
{
37
    public function initialize($intance, ServiceLocatorInterface $locator)
38
    {
39
        if ($intance instanceof ServiceTwoAwareInterface) {
40
            $intance->setService2($locator->get('service2'));
41
        }
42
    }
43
}
44
45
46
47
48
// setup ServiceLocator
49
$serviceLocator = new ServiceLocator();
50
51
// Hold with trait
52
$serviceLocator->addInitializer(new Service2Initializer());
53
54
// Hold with trait
55
$serviceLocator->setInvokable('service1', '\Example\ServiceCanHoldService2');
56
$serviceLocator->setInvokable('service2', '\Example\Service2');
57
58
59
// get Services
60
$service1 = $serviceLocator->get('service1');
61
62
if ($service1->getService2() !== $serviceLocator->get('service2')) {
63
    die('fail');
64
}
65
echo "done";
66