Passed
Push — master ( 39dd55...cb4ea4 )
by Mikael
02:08
created

InjectionMagicTrait::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Anax\DI;
4
5
use Anax\DI\Exception\Exception;
6
7
/**
8
 * Trait to use for DI aware services to let them know of the current $di.
9
 *
10
 */
11
trait InjectionMagicTrait
12
{
13
    /**
14
     * Magic method to get and create services.
15
     * When created it is also stored as a parameter of this object.
16
     *
17
     * @param string $service name of class property not existing.
18
     *
19
     * @throws NotFoundException  No entry was found for this identifier.
20
     *
21
     * @return object as the service requested.
22
     */
23
    public function __get($service)
24
    {
25
        return $this->find($service);
26
    }
27
28
29
30
   /**
31
     * Magic method to get and create services as a method call.
32
     * When created it is also stored as a parameter of this object.
33
     *
34
     * @param string $service   name of class property not existing.
35
     * @param array  $arguments Additional arguments to sen to the method
36
     *                          (NOT IMPLEMENTED).
37
     *
38
     * @throws NotFoundException  No entry was found for this identifier.
39
     *
40
     * @return class as the service requested.
41
     */
42
    public function __call($service, $arguments = [])
43
    {
44
        return $this->find($service);
45
    }
46
47
48
49
    /**
50
     * Find, load service and set as part of the class, then return it.
51
     *
52
     * @param string $service name of class property not existing.
53
     *
54
     * @throws NotFoundException  No entry was found for this identifier.
55
     *
56
     * @return object as the service requested.
57
     */
58
    private function find($service)
59
    {
60
        if (!$this->di) {
0 ignored issues
show
Documentation introduced by
The property di does not exist on object<Anax\DI\InjectionMagicTrait>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
            throw new Exception("InjectionAwareTrait \$di is not set. Call setDI()?");
62
        }
63
64
        $this->$service = $this->di->get($service);
0 ignored issues
show
Documentation introduced by
The property di does not exist on object<Anax\DI\InjectionMagicTrait>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
        return $this->$service;
66
    }
67
}
68