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

PropertyHelper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 51
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B isProperty() 0 24 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