Factory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A possibleFieldsReflections() 0 8 1
A buildResourceMeta() 0 15 2
A buildResourceAttributes() 0 10 2
A buildResourceRelationships() 0 10 2
A buildFieldByAttribute() 0 10 1
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 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...
10
use ReflectionObject;
11
use ReflectionMethod;
12
use ReflectionProperty;
13
use Reflector;
14
15
class Factory
16
{
17
    /**
18
     * @param object $resource
19
     * @return ResourceMeta
20
     */
21
    public function buildResourceMeta(object $resource): ResourceMeta
22
    {
23
        $ref = new \ReflectionObject($resource);
24
25
        /** @var AttrResource[] $attrs */
26
        $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

26
        $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...
27
28
        if (empty($attrs)) {
29
            throw new \InvalidArgumentException(sprintf(
30
                'The resource must have "%s" attribute.',
31
                AttrResource::class
32
            ));
33
        }
34
35
        return new ResourceMeta(current($attrs), $ref);
36
    }
37
38
    /**
39
     * @param object $resource
40
     *
41
     * @return Field[]
42
     */
43
    public function buildResourceAttributes(object $resource): array
44
    {
45
        $attributes = [];
46
47
        foreach ($this->possibleFieldsReflections($resource) as $ref) {
48
            $refAttributes = $this->buildFieldByAttribute($ref, Attribute::class);
49
            $attributes = array_merge($attributes, $refAttributes);
50
        }
51
52
        return $attributes;
53
    }
54
55
    /**
56
     * @param object $resource
57
     * @return Field[]
58
     */
59
    public function buildResourceRelationships(object $resource): array
60
    {
61
        $relationships = [];
62
63
        foreach ($this->possibleFieldsReflections($resource) as $ref) {
64
            $refRelationships = $this->buildFieldByAttribute($ref, Relationship::class);
65
            $relationships = array_merge($relationships, $refRelationships);
66
        }
67
68
        return $relationships;
69
    }
70
71
    /**
72
     * Find reflection attributes and generate metadata objects from it.
73
     *
74
     * @param ReflectionObject | ReflectionMethod | ReflectionProperty $ref
75
     * @param string $attributeClass
76
     * @return Field[]
77
     */
78
    private function buildFieldByAttribute(Reflector $ref, string $attributeClass): array
79
    {
80
        return array_map(
81
            // Create meta field
82
            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...
83
84
            // Find and convert reflection attribute to attribute instance.
85
            array_map(
86
                fn($attr) => $attr->newInstance(),
87
                $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

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