TypeHintsTrait::getSuggestedTypes()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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