Passed
Push — main ( ef112d...d39948 )
by Pieter
03:42
created

CompositeValueObjectWithExtension::with()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 15
rs 9.9332
1
<?php
2
3
4
namespace Apie\OpenapiSchema\Concerns;
5
6
use Apie\CompositeValueObjects\CompositeValueObjectTrait;
7
use Apie\CompositeValueObjects\Exceptions\FieldMissingException;
0 ignored issues
show
Bug introduced by
The type Apie\CompositeValueObjec...s\FieldMissingException 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 Apie\OpenapiSchema\ValueObjects\SpecificationExtension;
9
10
trait CompositeValueObjectWithExtension
11
{
12
    use CompositeValueObjectTrait {
13
        toNative as private internalToArray;
14
        fromNative as private internalFromArray;
15
        with as private internalWith;
16
    }
17
18
    /**
19
     * @var \Apie\OpenapiSchema\ValueObjects\SpecificationExtension|null
20
     */
21
    private $specificationExtension;
22
23
    public function toNative()
24
    {
25
        $result = $this->internalToArray();
26
        if (isset($result['specificationExtension'])) {
27
            $extensions = $result['specificationExtension'];
28
            unset($result['specificationExtension']);
29
            $result = array_merge($result, $extensions);
30
        }
31
        return array_filter(
32
            $result,
33
            function ($value) {
34
                return $value !== null;
35
            }
36
        );
37
    }
38
39
    public static function fromNative($input)
40
    {
41
        unset($input['specificationExtension']);
42
        $list = [];
43
        $list['specificationExtension'] = [];
44
        foreach ($input as $key => $value) {
45
            if (stripos($key, 'x-') === 0) {
46
                $list['specificationExtension'][$key] = $value;
47
            } else {
48
                $list[$key] = $value;
49
            }
50
        }
51
        return self::internalFromArray($list);
52
    }
53
54
    public function with(string $fieldName, $value): self
55
    {
56
        if ($fieldName === 'specificationExtension') {
57
            throw new FieldMissingException($fieldName, $this);
58
        }
59
        if (stripos($fieldName, 'x-') === 0) {
60
            $object = clone $this;
61
            if ($object->specificationExtension) {
62
                $object->specificationExtension = $this->specificationExtension->withField($fieldName, $value);
0 ignored issues
show
Bug introduced by
The method withField() does not exist on null. ( Ignorable by Annotation )

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

62
                /** @scrutinizer ignore-call */ 
63
                $object->specificationExtension = $this->specificationExtension->withField($fieldName, $value);

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...
63
            } else {
64
                $object->specificationExtension = new SpecificationExtension([$fieldName => $value]);
65
            }
66
            return $object;
67
        }
68
        return $this->internalWith($fieldName, $value);
69
    }
70
}