Passed
Push — main ( 62d82d...f77d7d )
by Pavel
01:32
created

Factory::buildFieldByAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php namespace JSONAPI\Resource\Metadata;
2
3
use JSONAPI\Resource\Attributes\AbstractFieldAttribute;
0 ignored issues
show
Bug introduced by
The type JSONAPI\Resource\Attributes\AbstractFieldAttribute was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use JSONAPI\Resource\Attributes\Attribute;
0 ignored issues
show
Bug introduced by
The type JSONAPI\Resource\Attributes\Attribute was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
6
use JSONAPI\Resource\Attributes\Relationship;
0 ignored issues
show
Bug introduced by
The type JSONAPI\Resource\Attributes\Relationship was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use JSONAPI\Resource\Attributes\Resource as AttrResource;
0 ignored issues
show
Bug introduced by
The type JSONAPI\Resource\Attributes\Resource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use JSONAPI\Resource\Metadata\Resource as ResourceMeta;
0 ignored issues
show
Bug introduced by
The type JSONAPI\Resource\Metadata\Resource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use JSONAPI\Resource\Tests\Models\People;
0 ignored issues
show
Bug introduced by
The type JSONAPI\Resource\Tests\Models\People was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use ReflectionAttribute;
0 ignored issues
show
Bug introduced by
The type ReflectionAttribute was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use ReflectionObject;
12
use ReflectionMethod;
13
use ReflectionProperty;
14
use Reflector;
15
16
class Factory
17
{
18
    /**
19
     * @param object $resource
20
     * @return ResourceMeta
21
     */
22
    public function buildResourceMeta(object $resource): ResourceMeta
23
    {
24
        $ref = new \ReflectionObject($resource);
25
26
        /** @var AttrResource[] $attrs */
27
        $attrs = array_map(fn($attr) => $attr->newInstance(), $ref->getAttributes(AttrResource::class));
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on ReflectionObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        $attrs = array_map(fn($attr) => $attr->newInstance(), $ref->/** @scrutinizer ignore-call */ getAttributes(AttrResource::class));

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...
28
29
        if (empty($attrs)) {
30
            throw new \InvalidArgumentException(sprintf(
31
                'The resource must have "%s" attribute.',
32
                AttrResource::class
33
            ));
34
        }
35
36
        return new ResourceMeta(current($attrs));
37
    }
38
39
    /**
40
     * @param object $resource
41
     *
42
     * @return Field[]
43
     */
44
    public function buildResourceAttributes(object $resource): array
45
    {
46
        $attributes = [];
47
48
        foreach ($this->possibleFieldsReflections($resource) as $ref) {
49
            $refAttributes = $this->buildFieldByAttribute($ref, Attribute::class);
50
            $attributes = array_merge($attributes, $refAttributes);
51
        }
52
53
        return $attributes;
54
    }
55
56
    /**
57
     * @param object $resource
58
     * @return Field[]
59
     */
60
    public function buildResourceRelationships(object $resource): array
61
    {
62
        $relationships = [];
63
64
        foreach ($this->possibleFieldsReflections($resource) as $ref) {
65
            $refRelationships = $this->buildFieldByAttribute($ref, Relationship::class);
66
            $relationships = array_merge($relationships, $refRelationships);
67
        }
68
69
        return $relationships;
70
    }
71
72
    /**
73
     * Find reflection attributes and generate metadata objects from it.
74
     *
75
     * @param ReflectionObject | ReflectionMethod | ReflectionProperty $ref
76
     * @param string $attributeClass
77
     * @return Field[]
78
     */
79
    private function buildFieldByAttribute(Reflector $ref, string $attributeClass): array
80
    {
81
        return array_map(
82
            // Create meta field
83
            fn(AbstractFieldAttribute $field) => new Field($field, $ref),
0 ignored issues
show
Bug introduced by
The type JSONAPI\Resource\Metadata\Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
84
85
            // Find and convert reflection attribute to attribute instance.
86
            array_map(
87
                fn($attr) => $attr->newInstance(),
88
                $ref->getAttributes($attributeClass, ReflectionAttribute::IS_INSTANCEOF)
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on Reflector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
                $ref->/** @scrutinizer ignore-call */ 
89
                      getAttributes($attributeClass, ReflectionAttribute::IS_INSTANCEOF)

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...
89
            ),
90
        );
91
    }
92
93
    /**
94
     * Return reflections of possible places where attribute can be assigned.
95
     *
96
     * @param object $resource
97
     * @return ReflectionObject[] | ReflectionMethod[] | ReflectionProperty[]
98
     */
99
    private function possibleFieldsReflections(object $resource): array
100
    {
101
        $objRef = new ReflectionObject($resource);
102
103
        return array_merge(
104
            [$objRef],
105
            $objRef->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC),
106
            $objRef->getProperties(ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PUBLIC),
107
        );
108
    }
109
}