VarTag::process()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 2
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\PhpdocParser\Tag\PhpDocumentor;
6
7
use Jasny\PhpdocParser\Tag\AbstractTag;
8
use Jasny\PhpdocParser\notation;
0 ignored issues
show
Bug introduced by
The type Jasny\PhpdocParser\notation 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 function Jasny\array_only;
10
11
/**
12
 * Custom logic for PhpDocumentor 'var', 'param' and 'property' tag
13
 */
14
class VarTag extends AbstractTag
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $additional;
20
21
    /**
22
     * @var callable|null
23
     */
24
    protected $fqsenConvertor;
25
26
    /**
27
     * Class constructor.
28
     *
29
     * @param string        $name            Tag name
30
     * @param callable|null $fqsenConvertor  Logic to convert class to FQCN
31
     * @param array         $additional      Additional properties
32
     */
33 6
    public function __construct(string $name, ?callable $fqsenConvertor = null, array $additional = [])
34
    {
35 6
        parent::__construct($name);
36
37 6
        $this->fqsenConvertor = $fqsenConvertor;
38 6
        $this->additional = $additional;
39
    }
40
41
    /**
42
     * Get additional properties that are always applied.
43
     *
44
     * @return array
45
     */
46 1
    public function getAdditionalProperties(): array
47
    {
48 1
        return $this->additional;
49
    }
50
51
    /**
52
     * Process a notation.
53
     *
54
     * @param array  $notations
55
     * @param string $value
56
     * @return array
57
     */
58 5
    public function process(array $notations, string $value): array
59
    {
60 5
        $regexp = '/^(?:(?<type>[^$\s]+)\s*)?(?:\$(?<name>\w+)\s*)?(?:"(?<id>[^"]+)"\s*)?(?:(?<description>.+))?/';
61 5
        preg_match($regexp, $value, $props); //regexp won't fail
62
63 5
        $this->removeEmptyValues($props);
64
65 5
        if (isset($props['type']) && isset($this->fqsenConvertor)) {
66 1
            $props['type'] = call_user_func($this->fqsenConvertor, $props['type']);
0 ignored issues
show
Bug introduced by
It seems like $this->fqsenConvertor can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

66
            $props['type'] = call_user_func(/** @scrutinizer ignore-type */ $this->fqsenConvertor, $props['type']);
Loading history...
67
        }
68
69 5
        $props = array_only($props, ['type', 'name', 'id', 'description']);
70
71 5
        $notations[$this->name] = $props + $this->additional;
72
73 5
        return $notations;
74
    }
75
76
    /**
77
     * Remove empty values from parsed data
78
     *
79
     * @param array $props
80
     */
81 5
    protected function removeEmptyValues(array &$props): void
82
    {
83 5
        foreach (['type', 'name', 'id'] as $name) {
84 5
            if (isset($props[$name]) && $props[$name] === '') {
85 2
                unset($props[$name]);
86
            }
87
        }
88
    }
89
}
90