Completed
Push — master ( 94992f...8033b7 )
by Mikael
03:17
created

DIMagicTrait::__call()   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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Anax\DI;
4
5
use Anax\DI\Exception\Exception;
6
7
/**
8
 * Trait to use to make a DI container use magic for
9
 * getting services. A class using a DI container can
10
 * access $this->di->session; as an alternative to
11
 * $this->di->get("session");
12
 */
13
trait DIMagicTrait
14
{
15
    /**
16
     * Magic method to get a service.
17
     *
18
     * @param string $service name of the service.
19
     *
20
     * @return mixed as the service requested.
21
     */
22
    public function __get($service)
23
    {
24
        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...
25
    }
26
27
28
29
    /**
30
     * Magic method to get and call a service.
31
     *
32
     * @param string $service name of the service.
33
     * @param array  $arguments currently NOT USED.
34
     *
35
     * @return mixed as the service requested.
36
     */
37 1
    public function __call($service, $arguments = [])
38
    {
39 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...
40
    }
41
}
42