Completed
Push — master ( 8cd077...c823b8 )
by Tomáš
14:59 queued 07:26
created

FileList::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * Represents a list of files on the file system that are to be checked during the run.
4
 *
5
 * File objects are created as needed rather than all at once.
6
 *
7
 * @author    Greg Sherwood <[email protected]>
8
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
9
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
10
 */
11
12
namespace PHP_CodeSniffer\Files;
13
14
use PHP_CodeSniffer\Util;
15
use PHP_CodeSniffer\Ruleset;
16
use PHP_CodeSniffer\Config;
17
18
class FileList implements \Iterator, \Countable
19
{
20
21
    /**
22
     * A list of file paths that are included in the list.
23
     *
24
     * @var array
25
     */
26
    private $files = array();
27
28
    /**
29
     * The number of files in the list.
30
     *
31
     * @var integer
32
     */
33
    private $numFiles = 0;
34
35
    /**
36
     * The config data for the run.
37
     *
38
     * @var \PHP_CodeSniffer\Config
39
     */
40
    public $config = null;
41
42
    /**
43
     * The ruleset used for the run.
44
     *
45
     * @var \PHP_CodeSniffer\Ruleset
46
     */
47
    public $ruleset = null;
48
49
    /**
50
     * An array of patterns to use for skipping files.
51
     *
52
     * @var array
53
     */
54
    protected $ignorePatterns = array();
55
56
57
    /**
58
     * Constructs a file list and loads in an array of file paths to process.
59
     *
60
     * @param \PHP_CodeSniffer\Config  $config  The config data for the run.
61
     * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
62
     *
63
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
64
     */
65
    public function __construct(Config $config, Ruleset $ruleset)
66
    {
67
        $this->ruleset = $ruleset;
68
        $this->config  = $config;
69
70
        $paths = $config->files;
0 ignored issues
show
Documentation introduced by
The property files does not exist on object<PHP_CodeSniffer\Config>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
        foreach ($paths as $path) {
72
            $this->addFile($path);
73
        }//end foreach
74
75
        reset($this->files);
76
        $this->numFiles = count($this->files);
77
78
    }//end __construct()
79
80
81
    /**
82
     * Add a file to the list.
83
     *
84
     * If a file object has already been created, it can be passed here.
85
     * If it is left NULL, it will be created when accessed.
86
     *
87
     * @param string                      $path The path to the file being added.
88
     * @param \PHP_CodeSniffer\Files\File $file The file being added.
89
     *
90
     * @return void
91
     */
92
    public function addFile($path, $file=null)
93
    {
94
        // No filtering is done for STDIN.
95
        if ($path === 'STDIN'
96
            || ($file !== null
97
            && get_class($file) === 'PHP_CodeSniffer\Files\DummyFile')
98
        ) {
99
            $this->files[$path] = $file;
100
            return;
101
        }
102
103
        $filterClass = $this->getFilterClass();
104
105
        $di       = new \RecursiveArrayIterator(array($path));
106
        $filter   = new $filterClass($di, $path, $this->config, $this->ruleset);
107
        $iterator = new \RecursiveIteratorIterator($filter);
108
109
        foreach ($iterator as $path) {
110
            $this->files[$path] = $file;
111
        }
112
113
    }//end addFile()
114
115
116
    /**
117
     * Get the class name of the filter being used for the run.
118
     *
119
     * @return string
120
     */
121
    private function getFilterClass()
122
    {
123
        $filterType = $this->config->filter;
0 ignored issues
show
Documentation introduced by
The property filter does not exist on object<PHP_CodeSniffer\Config>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
125
        if ($filterType === null) {
126
            $filterClass = '\PHP_CodeSniffer\Filters\Filter';
127 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
            if (strpos($filterType, '.') !== false) {
129
                // This is a path to a custom filter class.
130
                $filename = realpath($filterType);
131
                if ($filename === false) {
132
                    echo "ERROR: Custom filter \"$filterType\" not found".PHP_EOL;
133
                    exit(2);
0 ignored issues
show
Coding Style Compatibility introduced by
The method getFilterClass() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
134
                }
135
136
                $filterClass = \PHP_CodeSniffer\Autoload::loadFile($filename);
137
            } else {
138
                $filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType;
139
            }
140
        }
141
142
        return $filterClass;
143
144
    }//end getFilterClass()
145
146
147
    /**
148
     * Rewind the iterator to the first file.
149
     *
150
     * @return void
151
     */
152
    function rewind()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
    {
154
        reset($this->files);
155
156
    }//end rewind()
157
158
159
    /**
160
     * Get the file that is currently being processed.
161
     *
162
     * @return \PHP_CodeSniffer\Files\File
163
     */
164
    function current()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
165
    {
166
        $path = key($this->files);
167
        if ($this->files[$path] === null) {
168
            $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config);
169
        }
170
171
        return $this->files[$path];
172
173
    }//end current()
174
175
176
    /**
177
     * Return the file path of the current file being processed.
178
     *
179
     * @return void
180
     */
181
    function key()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
182
    {
183
        return key($this->files);
184
185
    }//end key()
186
187
188
    /**
189
     * Move forward to the next file.
190
     *
191
     * @return void
192
     */
193
    function next()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
194
    {
195
        next($this->files);
196
197
    }//end next()
198
199
200
    /**
201
     * Checks if current position is valid.
202
     *
203
     * @return boolean
204
     */
205
    function valid()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
206
    {
207
        if (current($this->files) === false) {
208
            return false;
209
        }
210
211
        return true;
212
213
    }//end valid()
214
215
216
    /**
217
     * Return the number of files in the list.
218
     *
219
     * @return integer
220
     */
221
    function count()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
222
    {
223
        return $this->numFiles;
224
225
    }//end count()
226
227
228
}//end class
229