Completed
Push — master ( 01dc8d...fb21bc )
by Emily
02:05
created

ReflectorFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 68.57%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 0
loc 87
ccs 24
cts 35
cp 0.6857
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B parseDocComment() 0 28 3
A setInt() 0 4 1
A setMixed() 0 4 1
A setBool() 0 19 4
1
<?php
2
3
namespace Spaark\CompositeUtils\Factory\Reflection;
4
5
use Spaark\CompositeUtils\Factory\BaseFactory;
6
use Spaark\CompositeUtils\Service\RawPropertyAccessor;
7
use Spaark\CompositeUtils\Model\Reflection\Reflector as SpaarkReflector;
8
use \Reflector as PHPNativeReflector;
9
10
abstract class ReflectorFactory extends BaseFactory
11
{
12
    const REFLECTION_OBJECT = null;
13
14
    /**
15
     * @var PHPNativeReflector
16
     */
17
    protected $reflector;
18
19
    /**
20
     * @var RawPropertyAccessor
21
     */
22
    protected $accessor;
23
24
    /**
25
     * @var SpaarkReflector
26
     */
27
    protected $object;
28
29 4
    public function __construct(PHPNativeReflector $reflector)
30
    {
31 4
        $class = static::REFLECTION_OBJECT;
32
33 4
        $this->object = new $class();
34 4
        $this->accessor = new RawPropertyAccessor($this->object);
35 4
        $this->reflector = $reflector;
36 4
    }
37
38 4
    protected function parseDocComment()
39
    {
40 4
        preg_match_all
41
        (
42
              '/^'
43
            .     '[ \t]*\*[ \t]*'
44
            .     '@([a-zA-Z]+)'
45
            .     '(.*)'
46 4
            . '$/m',
47 4
            $this->reflector->getDocComment(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Reflector as the method getDocComment() does only exist in the following implementations of said interface: ReflectionClass, ReflectionFunction, ReflectionFunctionAbstract, ReflectionMethod, ReflectionObject, ReflectionProperty.

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...
48
            $matches
49
        );
50
51 4
        foreach ($matches[0] as $key => $value)
52
        {
53 4
            $name = strtolower($matches[1][$key]);
54 4
            $value = trim($matches[2][$key]);
55
56 4
            if (isset($this->acceptedParams[$name]))
57
            {
58 4
                call_user_func
59
                (
60 4
                    array($this, $this->acceptedParams[$name]),
0 ignored issues
show
Bug introduced by
The property acceptedParams does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
                    $name, $value
62
                );
63
            }
64
        }
65 4
    }
66
67 4
    protected function setBool($name, $value)
68
    {
69 4
        switch(strtolower($value))
70
        {
71 4
            case '':
72
            case 'true':
73 4
                $value = true;
74 4
                break;
75
76
            case 'false':
77
                $value = false;
78
                break;
79
80
            default:
81
                $value = (boolean)$value;
82
        }
83
84 4
        $this->accessor->setRawValue($name, $value);
85 4
    }
86
87
    protected function setInt($name, $value)
88
    {
89
        $this->accessor->setRawValue($name, (int)$value);
90
    }
91
92
    protected function setMixed($name, $value)
93
    {
94
        $this->accessor->setRawValue($name, $value);
95
    }
96
}
97
98