Completed
Pull Request — master (#14)
by Brent
17:31
created

ValueObjectDefinition::resolveUseStatements()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
namespace Spatie\ValueObject;
4
5
use ReflectionClass as BaseReflectionClass;
6
use ReflectionProperty;
7
8
class ValueObjectDefinition extends BaseReflectionClass
9
{
10
    /** @var array */
11
    protected $uses = [];
12
13
    /** @var \Spatie\ValueObject\ValueObject */
14
    protected $valueObject;
15
16
    public function __construct(ValueObject $valueObject)
17
    {
18
        parent::__construct($valueObject);
19
20
        $this->valueObject = $valueObject;
21
22
        $this->resolveUseStatements();
23
    }
24
25
    protected function resolveUseStatements()
26
    {
27
        $handle = fopen($this->getFileName(), 'r');
28
29
        while ($line = fgets($handle)) {
30
            $line = trim($line);
31
32
            if (strpos($line, 'use ') !== 0) {
33
                continue;
34
            }
35
36
            $fqcn = str_replace(['use ', ';'], '', $line);
37
38
            $classParts = explode('\\', $fqcn);
39
40
            $alias = end($classParts);
41
42
            $this->uses[$alias] = $fqcn;
43
44
            if (strpos($line, 'class') !== false) {
45
                break;
46
            }
47
        }
48
49
        fclose($handle);
50
    }
51
52
    public function hasAlias(string $alias): bool
53
    {
54
        return isset($this->uses[$alias]);
55
    }
56
57
    public function resolveAlias(string $alias): string
58
    {
59
        return $this->uses[$alias];
60
    }
61
62
    /**
63
     * @return \Spatie\ValueObject\ValueObjectProperty[]
64
     */
65
    public function getValueObjectProperties(): array
66
    {
67
        $properties = [];
68
69
        foreach (parent::getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getProperties() instead of getValueObjectProperties()). Are you sure this is correct? If so, you might want to change this to $this->getProperties().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
70
            $properties[$reflectionProperty->getName()] = ValueObjectProperty::fromReflection(
71
                $this->valueObject,
72
                $this,
73
                $reflectionProperty
74
            );
75
        }
76
77
        return $properties;
78
    }
79
}
80