ClassMetadata::hasPropertyByRole()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Psi\Component\ContentType\Metadata;
4
5
use Metadata\MergeableClassMetadata;
6
use Metadata\PropertyMetadata;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Psi\Component\ContentTyp...tadata\PropertyMetadata.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Psi\Component\ContentType\Metadata\PropertyMetadata as PsiPropertyMetadata;
8
9
class ClassMetadata extends MergeableClassMetadata
10
{
11
    private $roles = [];
12
13
    public function __construct(
14
        $name
15
    ) {
16
        parent::__construct($name);
17
    }
18
19
    public function getName()
20
    {
21
        return $this->name;
22
    }
23
24
    public function getPropertyMetadata()
25
    {
26
        return $this->propertyMetadata;
27
    }
28
29
    public function addPropertyMetadata(PropertyMetadata $metadata)
30
    {
31
        $this->doAddPropertyMetadata($metadata);
0 ignored issues
show
Compatibility introduced by
$metadata of type object<Metadata\PropertyMetadata> is not a sub-type of object<Psi\Component\Con...adata\PropertyMetadata>. It seems like you assume a child class of the class Metadata\PropertyMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
32
    }
33
34
    public function hasPropertyByRole($role)
35
    {
36
        return isset($this->roles[$role]);
37
    }
38
39
    public function getPropertyByRole($role)
40
    {
41
        if (!isset($this->roles[$role])) {
42
            throw new \InvalidArgumentException(sprintf(
43
                'No property exists with role "%s"',
44
                $role
45
            ));
46
        }
47
48
        return $this->propertyMetadata[$this->roles[$role]];
49
    }
50
51
    private function doAddPropertyMetadata(PsiPropertyMetadata $metadata)
52
    {
53
        parent::addPropertyMetadata($metadata);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (addPropertyMetadata() instead of doAddPropertyMetadata()). Are you sure this is correct? If so, you might want to change this to $this->addPropertyMetadata().

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...
54
        if (!$metadata->getRole()) {
55
            return;
56
        }
57
58
        if (isset($this->roles[$metadata->getRole()])) {
59
            $propertyName = $this->roles[$metadata->getRole()];
60
            throw new \InvalidArgumentException(sprintf(
61
                'Role "%s" has already been assigned to property "%s" (on property "%s")',
62
                $metadata->getRole(), $propertyName, $metadata->getName()
63
            ));
64
        }
65
66
        $this->roles[$metadata->getRole()] = $metadata->name;
67
    }
68
}
69