Skip to content

Commit 743e2ad

Browse files
committed
4.0 | PHPCS Framework classes: add parameter types
1 parent 695ed86 commit 743e2ad

File tree

8 files changed

+59
-58
lines changed

8 files changed

+59
-58
lines changed

autoload.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Autoload
6565
*
6666
* @return bool
6767
*/
68-
public static function load($className)
68+
public static function load(string $className)
6969
{
7070
// Include the composer autoloader if there is one, but re-register it
7171
// so this autoloader runs before the composer one as we need to include
@@ -151,7 +151,7 @@ public static function load($className)
151151
*
152152
* @return string The fully qualified name of the class in the loaded file.
153153
*/
154-
public static function loadFile($path)
154+
public static function loadFile(string $path)
155155
{
156156
if (strpos(__DIR__, 'phar://') !== 0) {
157157
$path = realpath($path);
@@ -195,7 +195,7 @@ public static function loadFile($path)
195195
*
196196
* @return string The fully qualified name of the class in the loaded file.
197197
*/
198-
public static function determineLoadedClass($classesBeforeLoad, $classesAfterLoad)
198+
public static function determineLoadedClass(array $classesBeforeLoad, array $classesAfterLoad)
199199
{
200200
$className = null;
201201

@@ -256,7 +256,7 @@ static function ($remaining, $current) {
256256
*
257257
* @return void
258258
*/
259-
public static function addSearchPath($path, $nsPrefix='')
259+
public static function addSearchPath(string $path, string $nsPrefix='')
260260
{
261261
self::$searchPaths[$path] = rtrim(trim((string) $nsPrefix), '\\');
262262

@@ -283,7 +283,7 @@ public static function getSearchPaths()
283283
* @throws \Exception If the file path has not been loaded.
284284
* @return string
285285
*/
286-
public static function getLoadedClassName($path)
286+
public static function getLoadedClassName(string $path)
287287
{
288288
if (isset(self::$loadedClasses[$path]) === false) {
289289
throw new Exception("Cannot get class name for $path; file has not been included");
@@ -302,7 +302,7 @@ public static function getLoadedClassName($path)
302302
* @throws \Exception If the class name has not been loaded.
303303
* @return string
304304
*/
305-
public static function getLoadedFileName($className)
305+
public static function getLoadedFileName(string $className)
306306
{
307307
if (isset(self::$loadedFiles[$className]) === false) {
308308
throw new Exception("Cannot get file name for $className; class has not been included");

scripts/build-phar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
*
4747
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException When tokenizer errors are encountered.
4848
*/
49-
function stripWhitespaceAndComments($fullpath, $config)
49+
function stripWhitespaceAndComments(string $fullpath, Config $config)
5050
{
5151
$contents = file_get_contents($fullpath);
5252

src/Config.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class Config
252252
* @return mixed
253253
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the setting name is invalid.
254254
*/
255-
public function __get($name)
255+
public function __get(string $name)
256256
{
257257
if (array_key_exists($name, $this->settings) === false) {
258258
throw new RuntimeException("ERROR: unable to get value of property \"$name\"");
@@ -288,7 +288,7 @@ public function __get($name)
288288
* @return void
289289
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the setting name is invalid.
290290
*/
291-
public function __set($name, $value)
291+
public function __set(string $name, $value)
292292
{
293293
if (array_key_exists($name, $this->settings) === false) {
294294
throw new RuntimeException("Can't __set() $name; setting doesn't exist");
@@ -359,7 +359,7 @@ public function __set($name, $value)
359359
*
360360
* @return bool
361361
*/
362-
public function __isset($name)
362+
public function __isset(string $name)
363363
{
364364
return isset($this->settings[$name]);
365365

@@ -373,7 +373,7 @@ public function __isset($name)
373373
*
374374
* @return void
375375
*/
376-
public function __unset($name)
376+
public function __unset(string $name)
377377
{
378378
$this->settings[$name] = null;
379379

@@ -399,7 +399,7 @@ public function getSettings()
399399
*
400400
* @return void
401401
*/
402-
public function setSettings($settings)
402+
public function setSettings(array $settings)
403403
{
404404
$this->settings = $settings;
405405

@@ -415,7 +415,7 @@ public function setSettings($settings)
415415
*
416416
* @return void
417417
*/
418-
public function __construct(array $cliArgs=[], $dieOnUnknownArg=true)
418+
public function __construct(array $cliArgs=[], bool $dieOnUnknownArg=true)
419419
{
420420
if (defined('PHP_CODESNIFFER_IN_TESTS') === true) {
421421
// Let everything through during testing so that we can
@@ -498,7 +498,7 @@ public function __construct(array $cliArgs=[], $dieOnUnknownArg=true)
498498
*
499499
* @return void
500500
*/
501-
public function setCommandLineValues($args)
501+
public function setCommandLineValues(array $args)
502502
{
503503
$this->cliArgs = $args;
504504
$numArgs = count($args);
@@ -675,7 +675,7 @@ public function restoreDefaults()
675675
* @return void
676676
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
677677
*/
678-
public function processShortArgument($arg, $pos)
678+
public function processShortArgument(string $arg, int $pos)
679679
{
680680
switch ($arg) {
681681
case 'h':
@@ -787,7 +787,7 @@ public function processShortArgument($arg, $pos)
787787
* @return void
788788
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
789789
*/
790-
public function processLongArgument($arg, $pos)
790+
public function processLongArgument(string $arg, int $pos)
791791
{
792792
switch ($arg) {
793793
case 'help':
@@ -1319,7 +1319,7 @@ public function processLongArgument($arg, $pos)
13191319
* @return array<string>
13201320
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException When any of the provided codes are not valid as sniff codes.
13211321
*/
1322-
private function parseSniffCodes($input, $argument)
1322+
private function parseSniffCodes(string $input, string $argument)
13231323
{
13241324
$errors = [];
13251325
$sniffs = [];
@@ -1408,7 +1408,7 @@ static function ($carry, $item) {
14081408
* @return void
14091409
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
14101410
*/
1411-
public function processUnknownArgument($arg, $pos)
1411+
public function processUnknownArgument(string $arg, int $pos)
14121412
{
14131413
// We don't know about any additional switches; just files.
14141414
if ($arg[0] === '-') {
@@ -1434,7 +1434,7 @@ public function processUnknownArgument($arg, $pos)
14341434
* @return void
14351435
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
14361436
*/
1437-
public function processFilePath($path)
1437+
public function processFilePath(string $path)
14381438
{
14391439
// If we are processing STDIN, don't record any files to check.
14401440
if ($this->stdin === true) {
@@ -1490,7 +1490,7 @@ public function printUsage()
14901490
*
14911491
* @return string|void
14921492
*/
1493-
public function printShortUsage($returnOutput=false)
1493+
public function printShortUsage(bool $returnOutput=false)
14941494
{
14951495
if (PHP_CODESNIFFER_CBF === true) {
14961496
$usage = 'Run "phpcbf --help" for usage information';
@@ -1560,7 +1560,7 @@ public function printPHPCBFUsage()
15601560
* @see setConfigData()
15611561
* @see getAllConfigData()
15621562
*/
1563-
public static function getConfigData($key)
1563+
public static function getConfigData(string $key)
15641564
{
15651565
$phpCodeSnifferConfig = self::getAllConfigData();
15661566

@@ -1585,7 +1585,7 @@ public static function getConfigData($key)
15851585
* @return string|null
15861586
* @see getConfigData()
15871587
*/
1588-
public static function getExecutablePath($name)
1588+
public static function getExecutablePath(string $name)
15891589
{
15901590
$data = self::getConfigData($name.'_path');
15911591
if ($data !== null) {
@@ -1633,7 +1633,7 @@ public static function getExecutablePath($name)
16331633
* @see getConfigData()
16341634
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the config file can not be written.
16351635
*/
1636-
public function setConfigData($key, $value, $temp=false)
1636+
public function setConfigData(string $key, ?string $value, bool $temp=false)
16371637
{
16381638
if (isset($this->overriddenDefaults['runtime-set']) === true
16391639
&& isset($this->overriddenDefaults['runtime-set'][$key]) === true
@@ -1749,7 +1749,7 @@ public static function getAllConfigData()
17491749
*
17501750
* @return string
17511751
*/
1752-
public function prepareConfigDataForDisplay($data)
1752+
public function prepareConfigDataForDisplay(array $data)
17531753
{
17541754
if (empty($data) === true) {
17551755
return '';
@@ -1786,7 +1786,7 @@ public function prepareConfigDataForDisplay($data)
17861786
*
17871787
* @return void
17881788
*/
1789-
public function printConfigData($data)
1789+
public function printConfigData(array $data)
17901790
{
17911791
echo $this->prepareConfigDataForDisplay($data);
17921792

src/Fixer.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function fixFile()
235235
*
236236
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException When the diff command fails.
237237
*/
238-
public function generateDiff($filePath=null, $colors=true)
238+
public function generateDiff(?string $filePath=null, bool $colors=true)
239239
{
240240
if ($filePath === null) {
241241
$filePath = $this->currentFile->getFilename();
@@ -378,7 +378,7 @@ public function getContents()
378378
*
379379
* @return string
380380
*/
381-
public function getTokenContent($stackPtr)
381+
public function getTokenContent(int $stackPtr)
382382
{
383383
if ($this->inChangeset === true
384384
&& isset($this->changeset[$stackPtr]) === true
@@ -508,7 +508,7 @@ public function rollbackChangeset()
508508
*
509509
* @return bool If the change was accepted.
510510
*/
511-
public function replaceToken($stackPtr, $content)
511+
public function replaceToken(int $stackPtr, string $content)
512512
{
513513
if ($this->inConflict === true) {
514514
return false;
@@ -628,7 +628,7 @@ public function replaceToken($stackPtr, $content)
628628
*
629629
* @return bool If a change was reverted.
630630
*/
631-
public function revertToken($stackPtr)
631+
public function revertToken(int $stackPtr)
632632
{
633633
if (isset($this->fixedTokens[$stackPtr]) === false) {
634634
return false;
@@ -689,7 +689,7 @@ public function revertToken($stackPtr)
689689
*
690690
* @return bool If the change was accepted.
691691
*/
692-
public function substrToken($stackPtr, $start, $length=null)
692+
public function substrToken(int $stackPtr, int $start, ?int $length=null)
693693
{
694694
$current = $this->getTokenContent($stackPtr);
695695

@@ -711,7 +711,7 @@ public function substrToken($stackPtr, $start, $length=null)
711711
*
712712
* @return bool If the change was accepted.
713713
*/
714-
public function addNewline($stackPtr)
714+
public function addNewline(int $stackPtr)
715715
{
716716
$current = $this->getTokenContent($stackPtr);
717717
return $this->replaceToken($stackPtr, $current.$this->currentFile->eolChar);
@@ -726,7 +726,7 @@ public function addNewline($stackPtr)
726726
*
727727
* @return bool If the change was accepted.
728728
*/
729-
public function addNewlineBefore($stackPtr)
729+
public function addNewlineBefore(int $stackPtr)
730730
{
731731
$current = $this->getTokenContent($stackPtr);
732732
return $this->replaceToken($stackPtr, $this->currentFile->eolChar.$current);
@@ -742,7 +742,7 @@ public function addNewlineBefore($stackPtr)
742742
*
743743
* @return bool If the change was accepted.
744744
*/
745-
public function addContent($stackPtr, $content)
745+
public function addContent(int $stackPtr, string $content)
746746
{
747747
$current = $this->getTokenContent($stackPtr);
748748
return $this->replaceToken($stackPtr, $current.$content);
@@ -758,7 +758,7 @@ public function addContent($stackPtr, $content)
758758
*
759759
* @return bool If the change was accepted.
760760
*/
761-
public function addContentBefore($stackPtr, $content)
761+
public function addContentBefore(int $stackPtr, string $content)
762762
{
763763
$current = $this->getTokenContent($stackPtr);
764764
return $this->replaceToken($stackPtr, $content.$current);
@@ -778,7 +778,7 @@ public function addContentBefore($stackPtr, $content)
778778
*
779779
* @return void
780780
*/
781-
public function changeCodeBlockIndent($start, $end, $change)
781+
public function changeCodeBlockIndent(int $start, int $end, int $change)
782782
{
783783
$tokens = $this->currentFile->getTokens();
784784

@@ -835,7 +835,7 @@ public function changeCodeBlockIndent($start, $end, $change)
835835
*
836836
* @return string
837837
*/
838-
private function getSniffCodeForDebug($className)
838+
private function getSniffCodeForDebug(string $className)
839839
{
840840
try {
841841
$sniffCode = Common::getSniffCode($className);

src/Reporter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function __construct(Config $config)
187187
*
188188
* @return bool
189189
*/
190-
public function __isset($name)
190+
public function __isset(string $name)
191191
{
192192
return ($name === 'totalFixable' || $name === 'totalFixed');
193193

@@ -207,7 +207,7 @@ public function __isset($name)
207207
*
208208
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the setting name is invalid.
209209
*/
210-
public function __get($name)
210+
public function __get(string $name)
211211
{
212212
if ($name === 'totalFixable') {
213213
return ($this->totalFixableErrors + $this->totalFixableWarnings);
@@ -232,7 +232,7 @@ public function __get($name)
232232
*
233233
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
234234
*/
235-
public function __set($name, $value)
235+
public function __set(string $name, $value)
236236
{
237237
throw new RuntimeException("ERROR: setting property \"Reporter::\${$name}\" is not allowed");
238238

@@ -248,7 +248,7 @@ public function __set($name, $value)
248248
*
249249
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
250250
*/
251-
public function __unset($name)
251+
public function __unset(string $name)
252252
{
253253
throw new RuntimeException("ERROR: unsetting property \"Reporter::\${$name}\" is not allowed");
254254

@@ -286,7 +286,7 @@ public function printReports()
286286
*
287287
* @return void
288288
*/
289-
public function printReport($report)
289+
public function printReport(string $report)
290290
{
291291
$reportClass = $this->reports[$report]['class'];
292292
$reportFile = $this->reports[$report]['output'];

0 commit comments

Comments
 (0)