Skip to content

Commit fe82153

Browse files
committed
4.0 | Files: add parameter types
This commit explicitly does **not** include adding a parameter type to the `File::getTokensAsString()` `$start` and `$length` parameters as the pre-existing inline parameter validation is more comprehensive and more reliable.
1 parent 743e2ad commit fe82153

File tree

4 files changed

+81
-67
lines changed

4 files changed

+81
-67
lines changed

src/Files/DummyFile.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DummyFile extends File
3030
*
3131
* @return void
3232
*/
33-
public function __construct($content, Ruleset $ruleset, Config $config)
33+
public function __construct(string $content, Ruleset $ruleset, Config $config)
3434
{
3535
$this->setContent($content);
3636

@@ -71,8 +71,14 @@ public function __construct($content, Ruleset $ruleset, Config $config)
7171
*
7272
* @return void
7373
*/
74-
public function setErrorCounts($errorCount, $warningCount, $fixableErrorCount, $fixableWarningCount, $fixedErrorCount, $fixedWarningCount)
75-
{
74+
public function setErrorCounts(
75+
int $errorCount,
76+
int $warningCount,
77+
int $fixableErrorCount,
78+
int $fixableWarningCount,
79+
int $fixedErrorCount,
80+
int $fixedWarningCount
81+
) {
7682
$this->errorCount = $errorCount;
7783
$this->warningCount = $warningCount;
7884
$this->fixableErrorCount = $fixableErrorCount;

src/Files/File.php

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class File
267267
*
268268
* @return void
269269
*/
270-
public function __construct($path, Ruleset $ruleset, Config $config)
270+
public function __construct(string $path, Ruleset $ruleset, Config $config)
271271
{
272272
$this->path = $path;
273273
$this->ruleset = $ruleset;
@@ -296,7 +296,7 @@ public function __construct($path, Ruleset $ruleset, Config $config)
296296
*
297297
* @return void
298298
*/
299-
public function setContent($content)
299+
public function setContent(string $content)
300300
{
301301
$this->content = $content;
302302
$this->tokens = [];
@@ -671,12 +671,12 @@ public function cleanUp()
671671
* @return boolean
672672
*/
673673
public function addError(
674-
$error,
675-
$stackPtr,
676-
$code,
677-
$data=[],
678-
$severity=0,
679-
$fixable=false
674+
string $error,
675+
?int $stackPtr,
676+
string $code,
677+
array $data=[],
678+
int $severity=0,
679+
bool $fixable=false
680680
) {
681681
if ($stackPtr === null) {
682682
$line = 1;
@@ -705,12 +705,12 @@ public function addError(
705705
* @return boolean
706706
*/
707707
public function addWarning(
708-
$warning,
709-
$stackPtr,
710-
$code,
711-
$data=[],
712-
$severity=0,
713-
$fixable=false
708+
string $warning,
709+
?int $stackPtr,
710+
string $code,
711+
array $data=[],
712+
int $severity=0,
713+
bool $fixable=false
714714
) {
715715
if ($stackPtr === null) {
716716
$line = 1;
@@ -738,11 +738,11 @@ public function addWarning(
738738
* @return boolean
739739
*/
740740
public function addErrorOnLine(
741-
$error,
742-
$line,
743-
$code,
744-
$data=[],
745-
$severity=0
741+
string $error,
742+
int $line,
743+
string $code,
744+
array $data=[],
745+
int $severity=0
746746
) {
747747
return $this->addMessage(true, $error, $line, 1, $code, $data, $severity, false);
748748

@@ -762,11 +762,11 @@ public function addErrorOnLine(
762762
* @return boolean
763763
*/
764764
public function addWarningOnLine(
765-
$warning,
766-
$line,
767-
$code,
768-
$data=[],
769-
$severity=0
765+
string $warning,
766+
int $line,
767+
string $code,
768+
array $data=[],
769+
int $severity=0
770770
) {
771771
return $this->addMessage(false, $warning, $line, 1, $code, $data, $severity, false);
772772

@@ -788,11 +788,11 @@ public function addWarningOnLine(
788788
* @return boolean
789789
*/
790790
public function addFixableError(
791-
$error,
792-
$stackPtr,
793-
$code,
794-
$data=[],
795-
$severity=0
791+
string $error,
792+
int $stackPtr,
793+
string $code,
794+
array $data=[],
795+
int $severity=0
796796
) {
797797
$recorded = $this->addError($error, $stackPtr, $code, $data, $severity, true);
798798
if ($recorded === true && $this->fixer->enabled === true) {
@@ -819,11 +819,11 @@ public function addFixableError(
819819
* @return boolean
820820
*/
821821
public function addFixableWarning(
822-
$warning,
823-
$stackPtr,
824-
$code,
825-
$data=[],
826-
$severity=0
822+
string $warning,
823+
int $stackPtr,
824+
string $code,
825+
array $data=[],
826+
int $severity=0
827827
) {
828828
$recorded = $this->addWarning($warning, $stackPtr, $code, $data, $severity, true);
829829
if ($recorded === true && $this->fixer->enabled === true) {
@@ -850,8 +850,16 @@ public function addFixableWarning(
850850
*
851851
* @return boolean
852852
*/
853-
protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable)
854-
{
853+
protected function addMessage(
854+
bool $error,
855+
string $message,
856+
int $line,
857+
int $column,
858+
string $code,
859+
array $data,
860+
int $severity,
861+
bool $fixable
862+
) {
855863
// Check if this line is ignoring all message codes.
856864
if (isset($this->tokenizer->ignoredLines[$line]) === true && $this->tokenizer->ignoredLines[$line]->ignoresEverything() === true) {
857865
return false;
@@ -1097,7 +1105,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
10971105
*
10981106
* @return boolean
10991107
*/
1100-
public function recordMetric($stackPtr, $metric, $value)
1108+
public function recordMetric(int $stackPtr, string $metric, string $value)
11011109
{
11021110
if (isset($this->metrics[$metric]) === false) {
11031111
$this->metrics[$metric] = ['values' => [$value => 1]];
@@ -1311,7 +1319,7 @@ public function getFilename()
13111319
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type
13121320
* T_FUNCTION, T_CLASS, T_TRAIT, T_ENUM, or T_INTERFACE.
13131321
*/
1314-
public function getDeclarationName($stackPtr)
1322+
public function getDeclarationName(int $stackPtr)
13151323
{
13161324
$tokenCode = $this->tokens[$stackPtr]['code'];
13171325

@@ -1402,7 +1410,7 @@ public function getDeclarationName($stackPtr)
14021410
* type T_FUNCTION, T_CLOSURE, T_USE,
14031411
* or T_FN.
14041412
*/
1405-
public function getMethodParameters($stackPtr)
1413+
public function getMethodParameters(int $stackPtr)
14061414
{
14071415
if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
14081416
&& $this->tokens[$stackPtr]['code'] !== T_CLOSURE
@@ -1713,7 +1721,7 @@ public function getMethodParameters($stackPtr)
17131721
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
17141722
* T_FUNCTION, T_CLOSURE, or T_FN token.
17151723
*/
1716-
public function getMethodProperties($stackPtr)
1724+
public function getMethodProperties(int $stackPtr)
17171725
{
17181726
if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
17191727
&& $this->tokens[$stackPtr]['code'] !== T_CLOSURE
@@ -1901,7 +1909,7 @@ public function getMethodProperties($stackPtr)
19011909
* T_VARIABLE token, or if the position is not
19021910
* a class member variable.
19031911
*/
1904-
public function getMemberProperties($stackPtr)
1912+
public function getMemberProperties(int $stackPtr)
19051913
{
19061914
if ($this->tokens[$stackPtr]['code'] !== T_VARIABLE) {
19071915
throw new RuntimeException('$stackPtr must be of type T_VARIABLE');
@@ -2086,7 +2094,7 @@ public function getMemberProperties($stackPtr)
20862094
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
20872095
* T_CLASS token.
20882096
*/
2089-
public function getClassProperties($stackPtr)
2097+
public function getClassProperties(int $stackPtr)
20902098
{
20912099
if ($this->tokens[$stackPtr]['code'] !== T_CLASS) {
20922100
throw new RuntimeException('$stackPtr must be of type T_CLASS');
@@ -2143,7 +2151,7 @@ public function getClassProperties($stackPtr)
21432151
*
21442152
* @return boolean
21452153
*/
2146-
public function isReference($stackPtr)
2154+
public function isReference(int $stackPtr)
21472155
{
21482156
if ($this->tokens[$stackPtr]['code'] !== T_BITWISE_AND) {
21492157
return false;
@@ -2256,7 +2264,7 @@ public function isReference($stackPtr)
22562264
* @return string The token contents.
22572265
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position does not exist.
22582266
*/
2259-
public function getTokensAsString($start, $length, $origContent=false)
2267+
public function getTokensAsString($start, $length, bool $origContent=false)
22602268
{
22612269
if (is_int($start) === false || isset($this->tokens[$start]) === false) {
22622270
throw new RuntimeException('The $start position for getTokensAsString() must exist in the token stack');
@@ -2315,11 +2323,11 @@ public function getTokensAsString($start, $length, $origContent=false)
23152323
*/
23162324
public function findPrevious(
23172325
$types,
2318-
$start,
2319-
$end=null,
2320-
$exclude=false,
2321-
$value=null,
2322-
$local=false
2326+
int $start,
2327+
?int $end=null,
2328+
bool $exclude=false,
2329+
?string $value=null,
2330+
bool $local=false
23232331
) {
23242332
$types = (array) $types;
23252333

@@ -2396,11 +2404,11 @@ public function findPrevious(
23962404
*/
23972405
public function findNext(
23982406
$types,
2399-
$start,
2400-
$end=null,
2401-
$exclude=false,
2402-
$value=null,
2403-
$local=false
2407+
int $start,
2408+
?int $end=null,
2409+
bool $exclude=false,
2410+
?string $value=null,
2411+
bool $local=false
24042412
) {
24052413
$types = (array) $types;
24062414

@@ -2443,7 +2451,7 @@ public function findNext(
24432451
*
24442452
* @return int
24452453
*/
2446-
public function findStartOfStatement($start, $ignore=null)
2454+
public function findStartOfStatement(int $start, $ignore=null)
24472455
{
24482456
$startTokens = Tokens::BLOCK_OPENERS;
24492457
$startTokens[T_OPEN_SHORT_ARRAY] = true;
@@ -2634,7 +2642,7 @@ public function findStartOfStatement($start, $ignore=null)
26342642
*
26352643
* @return int
26362644
*/
2637-
public function findEndOfStatement($start, $ignore=null)
2645+
public function findEndOfStatement(int $start, $ignore=null)
26382646
{
26392647
$endTokens = [
26402648
T_COLON => true,
@@ -2761,7 +2769,7 @@ public function findEndOfStatement($start, $ignore=null)
27612769
* FALSE when no matching token could be found between the start of
27622770
* the line and the start token.
27632771
*/
2764-
public function findFirstOnLine($types, $start, $exclude=false, $value=null)
2772+
public function findFirstOnLine($types, int $start, bool $exclude=false, ?string $value=null)
27652773
{
27662774
if (is_array($types) === false) {
27672775
$types = [$types];
@@ -2811,7 +2819,7 @@ public function findFirstOnLine($types, $start, $exclude=false, $value=null)
28112819
*
28122820
* @return boolean
28132821
*/
2814-
public function hasCondition($stackPtr, $types)
2822+
public function hasCondition(int $stackPtr, $types)
28152823
{
28162824
// Check for the existence of the token.
28172825
if (isset($this->tokens[$stackPtr]) === false) {
@@ -2852,7 +2860,7 @@ public function hasCondition($stackPtr, $types)
28522860
*
28532861
* @return int|false
28542862
*/
2855-
public function getCondition($stackPtr, $type, $first=true)
2863+
public function getCondition(int $stackPtr, $type, bool $first=true)
28562864
{
28572865
// Check for the existence of the token.
28582866
if (isset($this->tokens[$stackPtr]) === false) {
@@ -2890,7 +2898,7 @@ public function getCondition($stackPtr, $type, $first=true)
28902898
*
28912899
* @return string|false
28922900
*/
2893-
public function findExtendedClassName($stackPtr)
2901+
public function findExtendedClassName(int $stackPtr)
28942902
{
28952903
// Check for the existence of the token.
28962904
if (isset($this->tokens[$stackPtr]) === false) {
@@ -2939,7 +2947,7 @@ public function findExtendedClassName($stackPtr)
29392947
*
29402948
* @return array|false
29412949
*/
2942-
public function findImplementedInterfaceNames($stackPtr)
2950+
public function findImplementedInterfaceNames(int $stackPtr)
29432951
{
29442952
// Check for the existence of the token.
29452953
if (isset($this->tokens[$stackPtr]) === false) {

src/Files/FileList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function __construct(Config $config, Ruleset $ruleset)
117117
*
118118
* @return void
119119
*/
120-
public function addFile($path, $file=null)
120+
public function addFile(string $path, ?File $file=null)
121121
{
122122
// No filtering is done for STDIN when the filename
123123
// has not been specified.

src/Files/LocalFile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class LocalFile extends File
2828
*
2929
* @return void
3030
*/
31-
public function __construct($path, Ruleset $ruleset, Config $config)
31+
public function __construct(string $path, Ruleset $ruleset, Config $config)
3232
{
3333
$this->path = trim($path);
3434
if (Common::isReadable($this->path) === false) {
@@ -166,7 +166,7 @@ public function process()
166166
*
167167
* @return void
168168
*/
169-
private function replayErrors($errors, $warnings)
169+
private function replayErrors(array $errors, array $warnings)
170170
{
171171
$this->errors = [];
172172
$this->warnings = [];

0 commit comments

Comments
 (0)