Skip to content

Commit 30b85c1

Browse files
committed
4.0 | Util classes: add parameter types
This commit explicitly does **not** include adding a parameter type to the `MessageCollector::add()` `$message` and `$type` parameters and the `Common::getSniffCode()` `$sniffsClass` parameter, as the pre-existing inline parameter validation is more comprehensive and more reliable. Includes removing a type check from an "internal-only" class which is no longer necessary (and updating the associated test).
1 parent b5ee6dc commit 30b85c1

File tree

9 files changed

+42
-52
lines changed

9 files changed

+42
-52
lines changed

src/Util/Cache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public static function save()
307307
*
308308
* @return mixed
309309
*/
310-
public static function get($key=null)
310+
public static function get(?string $key=null)
311311
{
312312
if ($key === null) {
313313
return self::$cache;
@@ -331,7 +331,7 @@ public static function get($key=null)
331331
*
332332
* @return void
333333
*/
334-
public static function set($key, $value)
334+
public static function set(?string $key, $value)
335335
{
336336
if ($key === null) {
337337
self::$cache = $value;

src/Util/Common.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Common
4949
*
5050
* @return bool
5151
*/
52-
public static function isPharFile($path)
52+
public static function isPharFile(string $path)
5353
{
5454
if (strpos($path, 'phar://') === 0) {
5555
return true;
@@ -70,7 +70,7 @@ public static function isPharFile($path)
7070
*
7171
* @return boolean
7272
*/
73-
public static function isReadable($path)
73+
public static function isReadable(string $path)
7474
{
7575
if (@is_readable($path) === true) {
7676
return true;
@@ -97,7 +97,7 @@ public static function isReadable($path)
9797
*
9898
* @return string|false
9999
*/
100-
public static function realpath($path)
100+
public static function realpath(string $path)
101101
{
102102
// Support the path replacement of ~ with the user's home directory.
103103
if (substr($path, 0, 2) === '~/') {
@@ -150,7 +150,7 @@ public static function realpath($path)
150150
*
151151
* @return string
152152
*/
153-
public static function stripBasepath($path, $basepath)
153+
public static function stripBasepath(string $path, ?string $basepath)
154154
{
155155
if (empty($basepath) === true) {
156156
return $path;
@@ -178,7 +178,7 @@ public static function stripBasepath($path, $basepath)
178178
*
179179
* @return string
180180
*/
181-
public static function detectLineEndings($contents)
181+
public static function detectLineEndings(string $contents)
182182
{
183183
if (preg_match("/\r\n?|\n/", $contents, $matches) !== 1) {
184184
// Assume there are no newlines.
@@ -258,7 +258,7 @@ public static function isStdinATTY()
258258
*
259259
* @return string
260260
*/
261-
public static function escapeshellcmd($cmd)
261+
public static function escapeshellcmd(string $cmd)
262262
{
263263
$cmd = escapeshellcmd($cmd);
264264

@@ -285,7 +285,7 @@ public static function escapeshellcmd($cmd)
285285
*
286286
* @return string
287287
*/
288-
public static function prepareForOutput($content, $exclude=[])
288+
public static function prepareForOutput(string $content, array $exclude=[])
289289
{
290290
if (PHP_OS_FAMILY === 'Windows') {
291291
if (in_array("\r", $exclude, true) === false) {
@@ -329,7 +329,7 @@ public static function prepareForOutput($content, $exclude=[])
329329
*
330330
* @return string
331331
*/
332-
public static function stripColors($text)
332+
public static function stripColors(string $text)
333333
{
334334
return preg_replace('`\033\[[0-9;]+m`', '', $text);
335335

@@ -357,10 +357,10 @@ public static function stripColors($text)
357357
* @return boolean
358358
*/
359359
public static function isCamelCaps(
360-
$name,
361-
$classFormat=false,
362-
$visibilityPublic=true,
363-
$strict=true
360+
string $name,
361+
bool $classFormat=false,
362+
bool $visibilityPublic=true,
363+
bool $strict=true
364364
) {
365365
// Check the first character first.
366366
if ($classFormat === false) {
@@ -428,7 +428,7 @@ public static function isCamelCaps(
428428
*
429429
* @return boolean
430430
*/
431-
public static function isUnderscoreName($name)
431+
public static function isUnderscoreName(string $name)
432432
{
433433
// If there is whitespace in the name, it can't be valid.
434434
if (strpos($name, ' ') !== false) {
@@ -469,7 +469,7 @@ public static function isUnderscoreName($name)
469469
*
470470
* @return string
471471
*/
472-
public static function suggestType($varType)
472+
public static function suggestType(string $varType)
473473
{
474474
if ($varType === '') {
475475
return '';
@@ -589,7 +589,7 @@ public static function getSniffCode($sniffClass)
589589
*
590590
* @return string
591591
*/
592-
public static function cleanSniffClass($sniffClass)
592+
public static function cleanSniffClass(string $sniffClass)
593593
{
594594
$newName = strtolower($sniffClass);
595595

src/Util/Help.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
namespace PHP_CodeSniffer\Util;
1818

19-
use InvalidArgumentException;
2019
use PHP_CodeSniffer\Config;
2120
use PHP_CodeSniffer\Util\Common;
2221

@@ -137,15 +136,9 @@ final class Help
137136
* @param \PHP_CodeSniffer\Config $config Configuration object.
138137
* @param array<string> $longOptions The long options which should be shown.
139138
* @param string $shortOptions The short options which should be shown.
140-
*
141-
* @throws \InvalidArgumentException When $shortOptions is not a string.
142139
*/
143-
public function __construct(Config $config, array $longOptions, $shortOptions='')
140+
public function __construct(Config $config, array $longOptions, string $shortOptions='')
144141
{
145-
if (is_string($shortOptions) === false) {
146-
throw new InvalidArgumentException('The $shortOptions parameter must be a string');
147-
}
148-
149142
$this->config = $config;
150143
$this->requestedOptions = array_merge($longOptions, str_split($shortOptions));
151144

@@ -327,7 +320,7 @@ private function printCategories()
327320
*
328321
* @return void
329322
*/
330-
private function printCategoryHeader($header)
323+
private function printCategoryHeader(string $header)
331324
{
332325
$header .= ':';
333326
if ($this->config->colors === true) {
@@ -393,7 +386,7 @@ private function printCategoryOptions(array $options)
393386
*
394387
* @return string
395388
*/
396-
private function colorizeVariableInput($text)
389+
private function colorizeVariableInput(string $text)
397390
{
398391
return preg_replace('`(<(?:(?>[^<>]+)|(?R))*>)`', "\033[36m".'$1'."\033[32m", $text);
399392

src/Util/IgnoreList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function getNewInstanceFrom(?IgnoreList $ignoreList)
9696
*
9797
* @return \PHP_CodeSniffer\Util\IgnoreList $this for chaining.
9898
*/
99-
public function set($code, $ignore)
99+
public function set(string $code, bool $ignore)
100100
{
101101
$data = &$this->data;
102102
$parts = explode('.', $code);
@@ -126,7 +126,7 @@ public function set($code, $ignore)
126126
*
127127
* @return bool
128128
*/
129-
public function isIgnored($code)
129+
public function isIgnored(string $code)
130130
{
131131
$data = $this->data;
132132
$returnValue = $data['.default'];

src/Util/MessageCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function containsBlockingErrors()
146146
*
147147
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException When there are blocking errors.
148148
*/
149-
public function display($order=self::ORDERBY_SEVERITY)
149+
public function display(string $order=self::ORDERBY_SEVERITY)
150150
{
151151
if ($this->cache === []) {
152152
return;
@@ -199,7 +199,7 @@ private function prefixAll(array $messages)
199199
*
200200
* @return string
201201
*/
202-
private function prefix($message, $type)
202+
private function prefix(string $message, int $type)
203203
{
204204
switch ($type) {
205205
case self::ERROR:

src/Util/Standards.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public static function getInstalledStandardPaths()
7777
* @see getInstalledStandardPaths()
7878
*/
7979
public static function getInstalledStandardDetails(
80-
$includeGeneric=false,
81-
$standardsDir=''
80+
bool $includeGeneric=false,
81+
string $standardsDir=''
8282
) {
8383
$rulesets = [];
8484

@@ -166,8 +166,8 @@ public static function getInstalledStandardDetails(
166166
* @see isInstalledStandard()
167167
*/
168168
public static function getInstalledStandards(
169-
$includeGeneric=false,
170-
$standardsDir=''
169+
bool $includeGeneric=false,
170+
string $standardsDir=''
171171
) {
172172
$installedStandards = [];
173173

@@ -231,7 +231,7 @@ public static function getInstalledStandards(
231231
* @return boolean
232232
* @see getInstalledStandards()
233233
*/
234-
public static function isInstalledStandard($standard)
234+
public static function isInstalledStandard(string $standard)
235235
{
236236
$path = self::getInstalledStandardPath($standard);
237237
if ($path !== null && strpos($path, 'ruleset.xml') !== false) {
@@ -277,7 +277,7 @@ public static function isInstalledStandard($standard)
277277
*
278278
* @return string|null
279279
*/
280-
public static function getInstalledStandardPath($standard)
280+
public static function getInstalledStandardPath(string $standard)
281281
{
282282
if (strpos($standard, '.') !== false) {
283283
return null;

src/Util/Timing.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function getDuration()
8989
*
9090
* @return float Duration in milliseconds.
9191
*/
92-
public static function getDurationSince($startTime)
92+
public static function getDurationSince(float $startTime)
9393
{
9494
return ((microtime(true) - $startTime) * 1000);
9595

@@ -103,7 +103,7 @@ public static function getDurationSince($startTime)
103103
*
104104
* @return string
105105
*/
106-
public static function getHumanReadableDuration($duration)
106+
public static function getHumanReadableDuration(float $duration)
107107
{
108108
$timeString = '';
109109
if ($duration >= self::MINUTE_IN_MS) {
@@ -132,7 +132,7 @@ public static function getHumanReadableDuration($duration)
132132
*
133133
* @return void
134134
*/
135-
public static function printRunTime($force=false)
135+
public static function printRunTime(bool $force=false)
136136
{
137137
if ($force === false && self::$printed === true) {
138138
// A double call.

src/Util/Writers/StatusWriter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ final class StatusWriter
6060
*
6161
* @return void
6262
*/
63-
public static function write($message, $indent=0, $newlines=1)
63+
public static function write(string $message, int $indent=0, int $newlines=1)
6464
{
6565
if (self::$paused === true) {
6666
return;
@@ -83,7 +83,7 @@ public static function write($message, $indent=0, $newlines=1)
8383
*
8484
* @return void
8585
*/
86-
public static function forceWrite($message, $indent=0, $newlines=1)
86+
public static function forceWrite(string $message, int $indent=0, int $newlines=1)
8787
{
8888
if ($indent > 0) {
8989
$message = str_repeat("\t", $indent).$message;
@@ -106,7 +106,7 @@ public static function forceWrite($message, $indent=0, $newlines=1)
106106
*
107107
* @return void
108108
*/
109-
public static function writeNewline($nr=1)
109+
public static function writeNewline(int $nr=1)
110110
{
111111
self::write('', 0, $nr);
112112

@@ -121,7 +121,7 @@ public static function writeNewline($nr=1)
121121
*
122122
* @return void
123123
*/
124-
public static function forceWriteNewline($nr=1)
124+
public static function forceWriteNewline(int $nr=1)
125125
{
126126
self::forceWrite('', 0, $nr);
127127

tests/Core/Util/Help/HelpTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Util\Help;
1111

12-
use InvalidArgumentException;
1312
use PHP_CodeSniffer\Tests\ConfigDouble;
1413
use PHP_CodeSniffer\Util\Help;
1514
use PHPUnit\Framework\TestCase;
1615
use ReflectionMethod;
1716
use ReflectionProperty;
17+
use TypeError;
1818

1919
/**
2020
* Test the Help class.
@@ -134,20 +134,17 @@ public function testQaValidCategoryOptionDefinitions()
134134

135135

136136
/**
137-
* Test receiving an expected exception when the shortOptions parameter is not passed a string value.
137+
* Test receiving an expected exception when the shortOptions parameter is not passed a scalar value.
138138
*
139139
* @return void
140140
*/
141-
public function testConstructorInvalidArgumentException()
141+
public function testConstructorTypeError()
142142
{
143-
$message = 'The $shortOptions parameter must be a string';
144-
145-
$this->expectException(InvalidArgumentException::class);
146-
$this->expectExceptionMessage($message);
143+
$this->expectException(TypeError::class);
147144

148145
new Help(new ConfigDouble(), [], []);
149146

150-
}//end testConstructorInvalidArgumentException()
147+
}//end testConstructorTypeError()
151148

152149

153150
/**

0 commit comments

Comments
 (0)