Test Failed
Pull Request — master (#2)
by Ashoka
03:06
created

TypeHintsTrait::isTypeObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
namespace Mediact\CodingStandard;
7
8
use PHP_CodeSniffer\Files\File;
0 ignored issues
show
Bug introduced by
The type PHP_CodeSniffer\Files\File 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 PHP_CodeSniffer\Util\Common;
0 ignored issues
show
Bug introduced by
The type PHP_CodeSniffer\Util\Common 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
11
trait TypeHintsTrait
12
{
13
    /**
14
     * Gets the type from a tag.
15
     *
16
     * @param File $file
17
     * @param int  $tagIndex
18
     *
19
     * @return string
20
     */
21
    protected function getTypeFromTag(File $file, $tagIndex)
22
    {
23
        $content = $file->getTokens()[($tagIndex + 2)]['content'];
24
        $parts   = explode(' ', $content, 2);
25
        $type    = $parts[0];
26
        return $type;
27
    }
28
29
    /**
30
     * Gets the suggested types based on a PHPDoc type.
31
     *
32
     * @param string $type
33
     *
34
     * @return array
35
     */
36
    protected function getSuggestedTypes($type)
37
    {
38
        $mapping = [
39
            'void'    => false,
40
            'mixed'   => false,
41
            'null'    => false,
42
            'int'     => 'int',
43
            'integer' => 'int',
44
            'string'  => 'string',
45
            'float'   => 'float',
46
            'bool'    => 'bool',
47
            'boolean' => 'bool',
48
        ];
49
50
        $typeNames = explode('|', $type);
51
        return array_unique(
52
            array_map(
53
                function ($typeName) use ($mapping) {
54
                    if (isset($mapping[$typeName])) {
55
                        return $mapping[$typeName];
56
                    }
57
58
                    if ($this->isTypeArray($typeName)) {
59
                        return 'array';
60
                    }
61
62
                    if ($this->isTypeCallable($typeName)) {
63
                        return 'callable';
64
                    }
65
66
                    if ($this->isTypeObject($typeName)) {
67
                        return $typeName;
68
                    }
69
70
                    return false;
71
                },
72
                $typeNames
73
            )
74
        );
75
    }
76
77
    /**
78
     * @param string $typeName
79
     *
80
     * @return bool
81
     */
82
    protected function isTypeArray($typeName)
83
    {
84
        return strpos($typeName, 'array') !== false
85
            || substr($typeName, -2) === '[]';
86
    }
87
88
    /**
89
     * @param string $typeName
90
     *
91
     * @return bool
92
     */
93
    protected function isTypeCallable($typeName)
94
    {
95
        return strpos($typeName, 'callable') !== false
96
            || strpos($typeName, 'callback') !== false;
97
    }
98
99
    /**
100
     * @param string $typeName
101
     *
102
     * @return bool
103
     */
104
    protected function isTypeObject($typeName)
105
    {
106
        return in_array($typeName, Common::$allowedTypes) === false;
107
    }
108
}
109