Completed
Pull Request — master (#11)
by Tomáš
05:11
created

Runner   D

Complexity

Total Complexity 81

Size/Duplication

Total Lines 539
Duplicated Lines 8.16 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 81
c 6
b 0
f 0
lcom 1
cbo 12
dl 44
loc 539
ccs 0
cts 321
cp 0
rs 4.0645

7 Methods

Rating   Name   Duplication   Size   Complexity  
C runPHPCS() 0 58 9
B runPHPCBF() 0 41 2
B init() 0 32 4
F run() 22 153 32
A handleErrors() 0 5 1
F processFile() 0 84 16
D processChildProcs() 22 75 17

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Runner often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Runner, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Responsible for running PHPCS and PHPCBF.
4
 *
5
 * After creating an object of this class, you probably just want to
6
 * call runPHPCS() or runPHPCBF().
7
 *
8
 * @author    Greg Sherwood <[email protected]>
9
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
10
 * @license   https://github.com/squizlabs/Symplify\PHP7_CodeSniffer/blob/master/licence.txt BSD Licence
11
 */
12
13
namespace Symplify\PHP7_CodeSniffer;
14
15
use Symplify\PHP7_CodeSniffer\Files\FileList;
16
use Symplify\PHP7_CodeSniffer\Files\File;
17
use Symplify\PHP7_CodeSniffer\Files\DummyFile;
18
use Symplify\PHP7_CodeSniffer\Util\Cache;
19
use Symplify\PHP7_CodeSniffer\Util\Common;
20
use Symplify\PHP7_CodeSniffer\Exceptions\RuntimeException;
21
22
class Runner
23
{
24
25
    /**
26
     * The config data for the run.
27
     *
28
     * @var \Symplify\PHP7_CodeSniffer\Config
29
     */
30
    public $config = null;
31
32
    /**
33
     * The ruleset used for the run.
34
     *
35
     * @var \Symplify\PHP7_CodeSniffer\Ruleset
36
     */
37
    public $ruleset = null;
38
39
    /**
40
     * The reporter used for generating reports after the run.
41
     *
42
     * @var \Symplify\PHP7_CodeSniffer\Reporter
43
     */
44
    public $reporter = null;
45
46
47
    /**
48
     * Run the PHPCS script.
49
     *
50
     * @return array
51
     */
52
    public function runPHPCS()
53
    {
54
        Util\Timing::startTiming();
55
56
        if (defined('PHP_CodeSniffer_CBF') === false) {
57
            define('PHP_CodeSniffer_CBF', false);
58
        }
59
60
        // Creating the Config object populates it with all required settings
61
        // based on the CLI arguments provided to the script and any config
62
        // values the user has set.
63
        $this->config = new Config();
64
65
        // Init the run and load the rulesets to set additional config vars.
66
        $this->init();
67
68
        // Print a list of sniffs in each of the supplied standards.
69
        // We fudge the config here so that each standard is explained in isolation.
70
        if ($this->config->explain === true) {
0 ignored issues
show
Documentation introduced by
The property explain does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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
            $standards = $this->config->standards;
0 ignored issues
show
Documentation introduced by
The property standards does not exist on object<Symplify\PHP7_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...
72
            foreach ($standards as $standard) {
73
                $this->config->standards = array($standard);
0 ignored issues
show
Documentation introduced by
The property standards does not exist on object<Symplify\PHP7_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...
74
                $ruleset = new Ruleset($this->config);
75
                $ruleset->explain();
76
            }
77
78
            exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method runPHPCS() 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...
79
        }
80
81
        // Disable caching if we are processing STDIN as we can't be 100%
82
        // sure where the file came from or if it will change in the future.
83
        if ($this->config->stdin === true) {
0 ignored issues
show
Documentation introduced by
The property stdin does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
84
            $this->config->cache = false;
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<Symplify\PHP7_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...
85
        }
86
87
        $numErrors = $this->run();
88
89
        // Print all the reports for this run.
90
        $toScreen = $this->reporter->printReports();
91
92
        // Only print timer output if no reports were
93
        // printed to the screen so we don't put additional output
94
        // in something like an XML report. If we are printing to screen,
95
        // the report types would have already worked out who should
96
        // print the timer info.
97
        if (($toScreen === false
98
            || (($this->reporter->totalErrors + $this->reporter->totalWarnings) === 0 && $this->config->showProgress === true))
0 ignored issues
show
Documentation introduced by
The property showProgress does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
99
        ) {
100
            Util\Timing::printRunTime();
101
        }
102
103
        if ($numErrors === 0) {
104
            exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method runPHPCS() 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...
105
        } else {
106
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method runPHPCS() 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...
107
        }
108
109
    }//end runPHPCS()
110
111
112
    /**
113
     * Run the PHPCBF script.
114
     *
115
     * @return array
116
     */
117
    public function runPHPCBF()
118
    {
119
        if (defined('PHP_CodeSniffer_CBF') === false) {
120
            define('PHP_CodeSniffer_CBF', true);
121
        }
122
123
        Util\Timing::startTiming();
124
125
        // Creating the Config object populates it with all required settings
126
        // based on the CLI arguments provided to the script and any config
127
        // values the user has set.
128
        $this->config = new Config();
129
130
        // Init the run and load the rulesets to set additional config vars.
131
        $this->init();
132
133
        // Override some of the command line settings that might break the fixes.
134
        $this->config->verbosity    = 0;
0 ignored issues
show
Documentation introduced by
The property verbosity does not exist on object<Symplify\PHP7_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...
135
        $this->config->showProgress = false;
0 ignored issues
show
Documentation introduced by
The property showProgress does not exist on object<Symplify\PHP7_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...
136
        $this->config->explain      = false;
0 ignored issues
show
Documentation introduced by
The property explain does not exist on object<Symplify\PHP7_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...
137
        $this->config->cache        = false;
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<Symplify\PHP7_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...
138
        $this->config->showSources  = false;
0 ignored issues
show
Documentation introduced by
The property showSources does not exist on object<Symplify\PHP7_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...
139
        $this->config->recordErrors = false;
0 ignored issues
show
Documentation introduced by
The property recordErrors does not exist on object<Symplify\PHP7_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...
140
        $this->config->reports      = array('cbf' => null);
0 ignored issues
show
Documentation introduced by
The property reports does not exist on object<Symplify\PHP7_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...
141
142
        // If a standard tries to set command line arguments itself, some
143
        // may be blocked because PHPCBF is running, so stop the script
144
        // dying if any are found.
145
        $this->config->dieOnUnknownArg = false;
146
147
        $numErrors = $this->run();
148
        $this->reporter->printReports();
149
150
        echo PHP_EOL;
151
        Util\Timing::printRunTime();
152
153
        // We can't tell exactly how many errors were fixed, but
154
        // we know how many errors were found.
155
        exit($numErrors);
0 ignored issues
show
Coding Style Compatibility introduced by
The method runPHPCBF() 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...
156
157
    }//end runPHPCBF()
158
159
160
    /**
161
     * Init the rulesets and other high-level settings.
162
     *
163
     * @return void
164
     */
165
    private function init()
166
    {
167
        // Ensure this option is enabled or else line endings will not always
168
        // be detected properly for files created on a Mac with the /r line ending.
169
        ini_set('auto_detect_line_endings', true);
170
171
        // Check that the standards are valid.
172
        foreach ($this->config->standards as $standard) {
0 ignored issues
show
Documentation introduced by
The property standards does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
173
            if (Util\Standards::isInstalledStandard($standard) === false) {
174
                // They didn't select a valid coding standard, so help them
175
                // out by letting them know which standards are installed.
176
                echo 'ERROR: the "'.$standard.'" coding standard is not installed. ';
177
                Util\Standards::printInstalledStandards();
178
                exit(2);
0 ignored issues
show
Coding Style Compatibility introduced by
The method init() 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...
179
            }
180
        }
181
182
        // Saves passing the Config object into other objects that only need
183
        // the verbostity flag for deubg output.
184
        if (defined('PHP_CodeSniffer_VERBOSITY') === false) {
185
            define('PHP_CodeSniffer_VERBOSITY', $this->config->verbosity);
0 ignored issues
show
Documentation introduced by
The property verbosity does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
186
        }
187
188
        // Create this class so it is autoloaded and sets up a bunch
189
        // of Symplify\PHP7_CodeSniffer-specific token type constants.
190
        $tokens = new Util\Tokens();
0 ignored issues
show
Unused Code introduced by
$tokens is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
191
192
        // The ruleset contains all the information about how the files
193
        // should be checked and/or fixed.
194
        $this->ruleset = new Ruleset($this->config);
195
196
    }//end init()
197
198
199
    /**
200
     * Performs the run.
201
     *
202
     * @return int The number of errors and warnings found.
203
     */
204
    private function run()
205
    {
206
        // The class that manages all reporters for the run.
207
        $this->reporter = new Reporter($this->config);
208
209
        if ($this->config->stdin === true) {
0 ignored issues
show
Documentation introduced by
The property stdin does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
210
            $fileContents = $this->config->stdinContent;
0 ignored issues
show
Documentation introduced by
The property stdinContent does not exist on object<Symplify\PHP7_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...
211
            if ($fileContents === null) {
212
                $handle = fopen('php://stdin', 'r');
213
                stream_set_blocking($handle, true);
214
                $fileContents = stream_get_contents($handle);
215
                fclose($handle);
216
            }
217
218
            $todo  = new FileList($this->config, $this->ruleset);
219
            $dummy = new DummyFile($fileContents, $this->ruleset, $this->config);
220
            $todo->addFile($dummy->path, $dummy);
221
222
            $numFiles = 1;
223
        } else {
224
            if (empty($this->config->files) === true) {
0 ignored issues
show
Documentation introduced by
The property files does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
225
                echo 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL;
226
                $this->config->printUsage();
227
                exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() 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...
228
            }
229
230
            if (PHP_CodeSniffer_VERBOSITY > 0) {
231
                echo 'Creating file list... ';
232
            }
233
234
            $todo     = new FileList($this->config, $this->ruleset);
235
            $numFiles = count($todo);
236
237
            if (PHP_CodeSniffer_VERBOSITY > 0) {
238
                echo "DONE ($numFiles files in queue)".PHP_EOL;
239
            }
240
241
            if ($this->config->cache === true) {
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
242
                if (PHP_CodeSniffer_VERBOSITY > 0) {
243
                    echo 'Loading cache... ';
244
                }
245
246
                Cache::load($this->ruleset, $this->config);
247
248
                if (PHP_CodeSniffer_VERBOSITY > 0) {
249
                    $size = Cache::getSize();
250
                    echo "DONE ($size files in cache)".PHP_EOL;
251
                }
252
            }
253
        }//end if
254
255
        $numProcessed = 0;
256
        $dots         = 0;
257
        $maxLength    = strlen($numFiles);
258
        $lastDir      = '';
259
        $childProcs   = array();
0 ignored issues
show
Unused Code introduced by
$childProcs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
260
261
        // Turn all sniff errors into exceptions.
262
        set_error_handler(array($this, 'handleErrors'));
263
264
        // Running normally.
265
        foreach ($todo as $path => $file) {
266
            $currDir = dirname($path);
267
            if ($lastDir !== $currDir) {
268
                if (PHP_CodeSniffer_VERBOSITY > 0 || (PHP_CodeSniffer_CBF === true && $this->config->stdin === false)) {
0 ignored issues
show
Documentation introduced by
The property stdin does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
269
                    echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL;
0 ignored issues
show
Documentation introduced by
The property basepath does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
270
                }
271
272
                $lastDir = $currDir;
273
            }
274
275
            $this->processFile($file);
276
277
            $numProcessed++;
278
279
            if (PHP_CodeSniffer_VERBOSITY > 0
280
                || $this->config->interactive === true
0 ignored issues
show
Documentation introduced by
The property interactive does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
281
                || $this->config->showProgress === false
0 ignored issues
show
Documentation introduced by
The property showProgress does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
282
            ) {
283
                continue;
284
            }
285
286
            // Show progress information.
287
            if ($file->ignored === true) {
288
                echo 'S';
289
            } else {
290
                $errors   = $file->getErrorCount();
291
                $warnings = $file->getWarningCount();
292 View Code Duplication
                if ($errors > 0) {
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...
293
                    if ($this->config->colors === true) {
0 ignored issues
show
Documentation introduced by
The property colors does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
294
                        echo "\033[31m";
295
                    }
296
297
                    echo 'E';
298
                } else if ($warnings > 0) {
299
                    if ($this->config->colors === true) {
0 ignored issues
show
Documentation introduced by
The property colors does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
300
                        echo "\033[33m";
301
                    }
302
303
                    echo 'W';
304
                } else {
305
                    echo '.';
306
                }
307
308
                if ($this->config->colors === true) {
0 ignored issues
show
Documentation introduced by
The property colors does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
309
                    echo "\033[0m";
310
                }
311
            }//end if
312
313
            $dots++;
314 View Code Duplication
            if ($dots === 60) {
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...
315
                $padding = ($maxLength - strlen($numProcessed));
316
                echo str_repeat(' ', $padding);
317
                $percent = round(($numProcessed / $numFiles) * 100);
318
                echo " $numProcessed / $numFiles ($percent%)".PHP_EOL;
319
                $dots = 0;
320
            }
321
        }//end foreach
322
323
        restore_error_handler();
324
325
        if (PHP_CodeSniffer_VERBOSITY === 0
326
            && $this->config->interactive === false
0 ignored issues
show
Documentation introduced by
The property interactive does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
327
            && $this->config->showProgress === true
0 ignored issues
show
Documentation introduced by
The property showProgress does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
328
        ) {
329
            echo PHP_EOL.PHP_EOL;
330
        }
331
332
        if ($this->config->cache === true) {
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
333
            Cache::save();
334
        }
335
336
        $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit');
337
        $ignoreErrors   = Config::getConfigData('ignore_errors_on_exit');
338
339
        $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings);
340
        if ($ignoreErrors !== null) {
341
            $ignoreErrors = (bool) $ignoreErrors;
342
            if ($ignoreErrors === true) {
343
                $return -= $this->reporter->totalErrors;
344
            }
345
        }
346
347
        if ($ignoreWarnings !== null) {
348
            $ignoreWarnings = (bool) $ignoreWarnings;
349
            if ($ignoreWarnings === true) {
350
                $return -= $this->reporter->totalWarnings;
351
            }
352
        }
353
354
        return $return;
355
356
    }//end run()
357
358
359
    /**
360
     * Converts all PHP errors into exceptions.
361
     *
362
     * This method forces a sniff to stop processing if it is not
363
     * able to handle a specific piece of code, instead of continuing
364
     * and potentially getting into a loop.
365
     *
366
     * @param int    $code    The level of error raised.
367
     * @param string $message The error message.
368
     * @param string $file    The path of the file that raised the error.
369
     * @param int    $line    The line number the error was raised at.
370
     *
371
     * @return void
372
     */
373
    public function handleErrors($code, $message, $file, $line)
374
    {
375
        throw new RuntimeException("$message in $file on line $line");
376
377
    }//end handleErrors()
378
379
380
    /**
381
     * Processes a single file, including checking and fixing.
382
     *
383
     * @param \Symplify\PHP7_CodeSniffer\Files\File $file The file to be processed.
384
     *
385
     * @return void
386
     */
387
    private function processFile($file)
388
    {
389
        if (PHP_CodeSniffer_VERBOSITY > 0 || (PHP_CodeSniffer_CBF === true && $this->config->stdin === false)) {
0 ignored issues
show
Documentation introduced by
The property stdin does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
390
            $startTime = microtime(true);
391
            echo 'Processing '.basename($file->path).' ';
392
            if (PHP_CodeSniffer_VERBOSITY > 1) {
393
                echo PHP_EOL;
394
            }
395
        }
396
397
        try {
398
            $file->process();
399
400
            if (PHP_CodeSniffer_VERBOSITY > 0
401
                || (PHP_CodeSniffer_CBF === true && $this->config->stdin === false)
0 ignored issues
show
Documentation introduced by
The property stdin does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
402
            ) {
403
                $timeTaken = ((microtime(true) - $startTime) * 1000);
0 ignored issues
show
Bug introduced by
The variable $startTime does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
404
                if ($timeTaken < 1000) {
405
                    $timeTaken = round($timeTaken);
406
                    echo "DONE in {$timeTaken}ms";
407
                } else {
408
                    $timeTaken = round(($timeTaken / 1000), 2);
409
                    echo "DONE in $timeTaken secs";
410
                }
411
412
                if (PHP_CodeSniffer_CBF === true) {
413
                    $errors = $file->getFixableCount();
414
                    echo " ($errors fixable violations)".PHP_EOL;
415
                } else {
416
                    $errors   = $file->getErrorCount();
417
                    $warnings = $file->getWarningCount();
418
                    echo " ($errors errors, $warnings warnings)".PHP_EOL;
419
                }
420
            }
421
        } catch (\Exception $e) {
422
            $error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage();
423
            $file->addErrorOnLine($error, 1, 'Internal.Exception');
424
        }//end try
425
426
        $this->reporter->cacheFileReport($file, $this->config);
0 ignored issues
show
Unused Code introduced by
The call to Reporter::cacheFileReport() has too many arguments starting with $this->config.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
427
428
        // Clean up the file to save (a lot of) memory.
429
        $file->cleanUp();
430
431
        if ($this->config->interactive === true) {
0 ignored issues
show
Documentation introduced by
The property interactive does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
432
            /*
433
                Running interactively.
434
                Print the error report for the current file and then wait for user input.
435
            */
436
437
            // Get current violations and then clear the list to make sure
438
            // we only print violations for a single file each time.
439
            $numErrors = null;
440
            while ($numErrors !== 0) {
441
                $numErrors = ($file->getErrorCount() + $file->getWarningCount());
442
                if ($numErrors === 0) {
443
                    continue;
444
                }
445
446
                $this->reporter->printReport('full');
447
448
                echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
449
                $input = fgets(STDIN);
450
                $input = trim($input);
451
452
                switch ($input) {
453
                case 's':
454
                    break(2);
455
                case 'q':
456
                    exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method processFile() 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...
457
                default:
458
                    // Repopulate the sniffs because some of them save their state
459
                    // and only clear it when the file changes, but we are rechecking
460
                    // the same file.
461
                    $file->ruleset->populateTokenListeners();
462
                    $file->reloadContent();
463
                    $file->process();
464
                    $this->reporter->cacheFileReport($file, $this->config);
0 ignored issues
show
Unused Code introduced by
The call to Reporter::cacheFileReport() has too many arguments starting with $this->config.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
465
                    break;
466
                }
467
            }//end while
468
        }//end if
469
470
    }//end processFile()
471
472
473
    /**
474
     * Waits for child processes to complete and cleans up after them.
475
     *
476
     * The reporting information returned by each child process is merged
477
     * into the main reporter class.
478
     *
479
     * @param array $childProcs An array of child processes to wait for.
480
     *
481
     * @return void
482
     */
483
    private function processChildProcs($childProcs)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
484
    {
485
        $dots         = 0;
486
        $numProcessed = 0;
487
        $totalBatches = count($childProcs);
488
        $maxLength    = strlen($totalBatches);
489
490
        while (count($childProcs) > 0) {
491
            foreach ($childProcs as $key => $procData) {
492
                $res = pcntl_waitpid($procData['pid'], $status, WNOHANG);
493
                if ($res === $procData['pid']) {
494
                    if (file_exists($procData['out']) === true) {
495
                        include $procData['out'];
496
                        if (isset($childOutput) === true) {
497
                            $this->reporter->totalFiles    += $childOutput['totalFiles'];
0 ignored issues
show
Bug introduced by
The variable $childOutput does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
498
                            $this->reporter->totalErrors   += $childOutput['totalErrors'];
499
                            $this->reporter->totalWarnings += $childOutput['totalWarnings'];
500
                            $this->reporter->totalFixable  += $childOutput['totalFixable'];
501
                        }
502
503
                        if (isset($debugOutput) === true) {
504
                            echo $debugOutput;
0 ignored issues
show
Bug introduced by
The variable $debugOutput does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
505
                        }
506
507
                        if (isset($childCache) === true) {
508
                            foreach ($childCache as $path => $cache) {
0 ignored issues
show
Bug introduced by
The variable $childCache does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
509
                                Cache::set($path, $cache);
510
                            }
511
                        }
512
513
                        unlink($procData['out']);
514
                        unset($childProcs[$key]);
515
516
                        $numProcessed++;
517
518
                        if (PHP_CodeSniffer_VERBOSITY > 0
519
                            || $this->config->showProgress === false
0 ignored issues
show
Documentation introduced by
The property showProgress does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
520
                        ) {
521
                            continue;
522
                        }
523
524 View Code Duplication
                        if ($childOutput['totalErrors'] > 0) {
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...
525
                            if ($this->config->colors === true) {
0 ignored issues
show
Documentation introduced by
The property colors does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
526
                                echo "\033[31m";
527
                            }
528
529
                            echo 'E';
530
                        } else if ($childOutput['totalWarnings'] > 0) {
531
                            if ($this->config->colors === true) {
0 ignored issues
show
Documentation introduced by
The property colors does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
532
                                echo "\033[33m";
533
                            }
534
535
                            echo 'W';
536
                        } else {
537
                            echo '.';
538
                        }
539
540
                        if ($this->config->colors === true) {
0 ignored issues
show
Documentation introduced by
The property colors does not exist on object<Symplify\PHP7_CodeSniffer\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
541
                            echo "\033[0m";
542
                        }
543
544
                        $dots++;
545 View Code Duplication
                        if ($dots === 60) {
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...
546
                            $padding = ($maxLength - strlen($numProcessed));
547
                            echo str_repeat(' ', $padding);
548
                            $percent = round(($numProcessed / $totalBatches) * 100);
549
                            echo " $numProcessed / $totalBatches ($percent%)".PHP_EOL;
550
                            $dots = 0;
551
                        }
552
                    }//end if
553
                }//end if
554
            }//end foreach
555
        }//end while
556
557
    }//end processChildProcs()
558
559
560
}//end class
561