GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3684b5...a74bc8 )
by Sergey
03:58
created

AnnotationLoader::loadResourceMetadata()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 5
eloc 10
nc 5
nop 2
1
<?php
2
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) Sergey Revenko <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Reva2\JsonApi\Decoders\Mapping\Loader;
13
14
use Doctrine\Common\Annotations\Reader;
15
use Reva2\JsonApi\Annotations\Attribute;
16
use Reva2\JsonApi\Annotations\Document as ApiDocument;
17
use Reva2\JsonApi\Annotations\Relationship;
18
use Reva2\JsonApi\Annotations\Resource as ApiResource;
19
use Reva2\JsonApi\Contracts\Decoders\Mapping\Loader\LoaderInterface;
20
use Reva2\JsonApi\Decoders\Mapping\AttributeMetadata;
21
use Reva2\JsonApi\Decoders\Mapping\DocumentMetadata;
22
use Reva2\JsonApi\Decoders\Mapping\GenericMetadata;
23
use Reva2\JsonApi\Decoders\Mapping\ResourceMetadata;
24
25
/**
26
 * Loads JSON API metadata using a Doctrine annotations
27
 *
28
 * @package Reva2\JsonApi\Decoders\Mapping\Loader
29
 * @author Sergey Revenko <[email protected]>
30
 */
31
class AnnotationLoader implements LoaderInterface
32
{
33
    /**
34
     * @var Reader
35
     */
36
    protected $reader;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param Reader $reader
42
     */
43
    public function __construct(Reader $reader)
44
    {
45
        $this->reader = $reader;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function loadClassMetadata(\ReflectionClass $class)
52
    {
53
        if (null !== ($resource = $this->reader->getClassAnnotation($class, ApiResource::class))) {
54
            return $this->loadResourceMetadata($resource, $class);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->loadResour...ata($resource, $class); (Reva2\JsonApi\Decoders\Mapping\ResourceMetadata) is incompatible with the return type declared by the interface Reva2\JsonApi\Contracts\...face::loadClassMetadata of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
55
        } elseif (null !== ($document = $this->reader->getClassAnnotation($class, ApiDocument::class))) {
56
            return $this->loadDocumentMetadata($document, $class);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->loadDocume...ata($document, $class); (Reva2\JsonApi\Decoders\Mapping\DocumentMetadata) is incompatible with the return type declared by the interface Reva2\JsonApi\Contracts\...face::loadClassMetadata of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
57
        } else {
58
            return new GenericMetadata($class->getName());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Reva2\JsonAp...ata($class->getName()); (Reva2\JsonApi\Decoders\Mapping\GenericMetadata) is incompatible with the return type declared by the interface Reva2\JsonApi\Contracts\...face::loadClassMetadata of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
59
        }
60
    }
61
62
    /**
63
     * Parse JSON API resource metadata
64
     *
65
     * @param ApiResource $resource
66
     * @param \ReflectionClass $class
67
     * @return ResourceMetadata
68
     */
69
    private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class)
70
    {
71
        $metadata = new ResourceMetadata($resource->name, $class->getName());
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
72
73
        $properties = $class->getProperties();
74
        foreach ($properties as $property) {
75
            foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
76
                if ($annotation instanceof Attribute) {
77
                    $metadata->addAttribute($this->parseAttribute($annotation, $property));
0 ignored issues
show
Documentation introduced by
$this->parseAttribute($annotation, $property) is of type null, but the function expects a object<Reva2\JsonApi\Con...ibuteMetadataInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
                } elseif ($annotation instanceof Relationship) {
79
                    $metadata->addRelationship($this->parseRelationship($annotation, $property));
0 ignored issues
show
Bug introduced by
The method parseRelationship() does not seem to exist on object<Reva2\JsonApi\Dec...oader\AnnotationLoader>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
                }
81
            }
82
        }
83
84
        return $metadata;
85
    }
86
87
    /**
88
     * Parse JSON API document metadata
89
     *
90
     * @param ApiDocument $document
91
     * @param \ReflectionClass $class
92
     * @return DocumentMetadata
93
     */
94
    private function loadDocumentMetadata(ApiDocument $document, \ReflectionClass $class)
0 ignored issues
show
Unused Code introduced by
The parameter $document is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        $metadata = new DocumentMetadata($class->getName());
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
97
98
        return $metadata;
99
    }
100
101
    private function parseAttribute(Attribute $attribute, \ReflectionProperty $property)
102
    {
103
        $metadata = new AttributeMetadata($property->getName(), $property->class);
104
        if ($attribute->type) {
105
            $this->parseAttributeType($attribute->type, $metadata);
0 ignored issues
show
Bug introduced by
The method parseAttributeType() does not exist on Reva2\JsonApi\Decoders\M...Loader\AnnotationLoader. Did you maybe mean parseAttribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
106
        } else {
107
            $this->parsePropertyDataType($property, $metadata);
0 ignored issues
show
Bug introduced by
The method parsePropertyDataType() does not seem to exist on object<Reva2\JsonApi\Dec...oader\AnnotationLoader>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        }
109
        
110
    }
111
}