Completed
Push — master ( 56e75a...611edb )
by Mikael
01:56
created

DIMagicTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 29
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 4 1
A __call() 0 4 1
1
<?php
2
namespace Anax\DI;
3
4
use Anax\DI\Exception\Exception;
5
6
/**
7
 * Trait to use to make a DI container use magic for
8
 * getting services. A class using a DI container can
9
 * access $this->di->session; as an alternative to
10
 * $this->di->get("session");
11
 */
12
trait DIMagicTrait
13
{
14
    /**
15
     * Magic method to get a service.
16
     *
17
     * @param string $service name of the service.
18
     *
19
     * @return mixed as the service requested.
20
     */
21 1
    public function __get($service)
22
    {
23 1
        return $this->get($service);
0 ignored issues
show
Documentation Bug introduced by
The method get does not exist on object<Anax\DI\DIMagicTrait>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
24
    }
25
26
27
28
    /**
29
     * Magic method to get and call a service.
30
     *
31
     * @param string $service name of the service.
32
     * @param array  $arguments currently NOT USED.
33
     *
34
     * @return mixed as the service requested.
35
     */
36 1
    public function __call($service, $arguments = [])
37
    {
38 1
        return $this->get($service);
0 ignored issues
show
Documentation Bug introduced by
The method get does not exist on object<Anax\DI\DIMagicTrait>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
39
    }
40
}
41