Completed
Pull Request — master (#19)
by
unknown
07:50
created

PropertyHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\CodeSniffer\Helper;
6
7
use BestIt\CodeSniffer\File;
8
9
/**
10
 * Class PropertyHelper
11
 *
12
 * @package BestIt\Helper
13
 * @author Nick Lubisch <[email protected]>
14
 */
15
final class PropertyHelper
16
{
17
    /**
18
     * The wrapped PHP_CodeSniffer_File
19
     *
20
     * @var File
21
     */
22
    private $file;
23
24
    /**
25
     * PropertyHelper constructor.
26
     *
27
     * @param File $file The wrapped PHP_CodeSniffer_File
28
     */
29 121
    public function __construct(File $file)
30
    {
31 121
        $this->file = $file;
32 121
    }
33
34
    /**
35
     * Determines if the given variable is a property of a class.
36
     *
37
     * @param int $variablePtr Pointer to the current variable
38
     *
39
     * @return bool Indicator if the current T_VARIABLE is a property of a class
40
     */
41 26
    public function isProperty(int $variablePtr): bool
42
    {
43 26
        $tokens = $this->file->getTokens();
44
45 26
        $propertyPointer = $this->file->findPrevious(
46 26
            [T_STATIC, T_WHITESPACE, T_COMMENT],
47 26
            $variablePtr - 1,
48 26
            null,
49 26
            true
50
        );
51 26
        $propertyToken = $tokens[$propertyPointer];
52 26
        $propertyCode = $propertyToken['code'];
53
54 26
        return in_array(
55 26
            $propertyCode,
56
            [
57 26
                T_PRIVATE,
58 26
                T_PROTECTED,
59 26
                T_PUBLIC,
60 26
                T_VAR
61
            ],
62 26
            true
63
        );
64
    }
65
}
66