Skip to content

Commit 76cf6a2

Browse files
committed
coding style
1 parent e5a098b commit 76cf6a2

20 files changed

Lines changed: 125 additions & 124 deletions

ncs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<rule ref="$presets/php72.xml"/>
44

55
<rule ref="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly">
6-
<severity>0</severity>
6+
<exclude-pattern>./tests/*/*.phpt</exclude-pattern>
77
</rule>
88
</ruleset>

src/CodeCoverage/Collector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static function start(string $file, string $engine): void
5555

5656
} elseif (!in_array(
5757
$engine,
58-
array_map(function (array $engineInfo) { return $engineInfo[0]; }, self::detectEngines()),
58+
array_map(fn(array $engineInfo) => $engineInfo[0], self::detectEngines()),
5959
true
6060
)) {
6161
throw new \LogicException("Code coverage engine '$engine' is not supported.");

src/Framework/Assert.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static function contains($needle, $actual, ?string $description = null):
121121
if (!is_string($needle)) {
122122
self::fail(self::describe('Needle %1 should be string'), $needle);
123123

124-
} elseif ($needle !== '' && strpos($actual, $needle) === false) {
124+
} elseif ($needle !== '' && !str_contains($actual, $needle)) {
125125
self::fail(self::describe('%1 should contain %2', $description), $actual, $needle);
126126
}
127127
} else {
@@ -146,7 +146,7 @@ public static function notContains($needle, $actual, ?string $description = null
146146
if (!is_string($needle)) {
147147
self::fail(self::describe('Needle %1 should be string'), $needle);
148148

149-
} elseif ($needle === '' || strpos($actual, $needle) !== false) {
149+
} elseif ($needle === '' || str_contains($actual, $needle)) {
150150
self::fail(self::describe('%1 should not contain %2', $description), $actual, $needle);
151151
}
152152
} else {
@@ -316,8 +316,8 @@ public static function type($type, $value, ?string $description = null): void
316316
self::fail(self::describe(gettype($value) . " should be $type", $description));
317317
}
318318
} elseif (!$value instanceof $type) {
319-
$actual = is_object($value) ? get_class($value) : gettype($value);
320-
$type = is_object($type) ? get_class($type) : $type;
319+
$actual = is_object($value) ? $value::class : gettype($value);
320+
$type = is_object($type) ? $type::class : $type;
321321
self::fail(self::describe("$actual should be instance of $type", $description));
322322
}
323323
}
@@ -331,7 +331,8 @@ public static function exception(
331331
string $class,
332332
?string $message = null,
333333
$code = null
334-
): ?\Throwable {
334+
): ?\Throwable
335+
{
335336
self::$counter++;
336337
$e = null;
337338
try {
@@ -343,7 +344,7 @@ public static function exception(
343344
self::fail("$class was expected, but none was thrown");
344345

345346
} elseif (!$e instanceof $class) {
346-
self::fail("$class was expected but got " . get_class($e) . ($e->getMessage() ? " ({$e->getMessage()})" : ''), null, null, $e);
347+
self::fail("$class was expected but got " . $e::class . ($e->getMessage() ? " ({$e->getMessage()})" : ''), null, null, $e);
347348

348349
} elseif ($message && !self::isMatching($message, $e->getMessage())) {
349350
self::fail("$class with a message matching %2 was expected but got %1", $e->getMessage(), $message, $e);
@@ -506,7 +507,8 @@ public static function fail(
506507
$expected = null,
507508
?\Throwable $previous = null,
508509
?string $outputName = null
509-
): void {
510+
): void
511+
{
510512
$e = new AssertException($message, $expected, $actual, $previous);
511513
$e->outputName = $outputName;
512514
if (self::$onFailure) {
@@ -587,10 +589,10 @@ public static function expandMatchingPatterns(string $pattern, $actual): array
587589

588590
$parts = preg_split('#(%)#', $pattern, -1, PREG_SPLIT_DELIM_CAPTURE);
589591
for ($i = count($parts); $i >= 0; $i--) {
590-
$patternX = implode(array_slice($parts, 0, $i));
592+
$patternX = implode('', array_slice($parts, 0, $i));
591593
$patternY = "$patternX%A?%";
592594
if (self::isMatching($patternY, $actual)) {
593-
$patternZ = implode(array_slice($parts, $i));
595+
$patternZ = implode('', array_slice($parts, $i));
594596
break;
595597
}
596598
}
@@ -654,7 +656,7 @@ private static function isEqual($expected, $actual, int $level = 0, $objects = n
654656
$diff = abs($expected - $actual);
655657
return ($diff < self::Epsilon) || ($diff / max(abs($expected), abs($actual)) < self::Epsilon);
656658

657-
case is_object($expected) && is_object($actual) && get_class($expected) === get_class($actual):
659+
case is_object($expected) && is_object($actual) && $expected::class === $actual::class:
658660
$objects = $objects ? clone $objects : new \SplObjectStorage;
659661
if (isset($objects[$expected])) {
660662
return $objects[$expected] === $actual;

src/Framework/DataProvider.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public static function load(string $file, string $query = ''): array
2626
}
2727

2828
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
29-
$data = (function () {
30-
return require func_get_arg(0);
31-
})(realpath($file));
29+
$data = (fn() => require func_get_arg(0))(realpath($file));
3230

3331
if ($data instanceof \Traversable) {
3432
$data = iterator_to_array($data);

src/Framework/DomQuery.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ class DomQuery extends \SimpleXMLElement
1717
{
1818
public static function fromHtml(string $html): self
1919
{
20-
if (strpos($html, '<') === false) {
20+
if (!str_contains($html, '<')) {
2121
$html = '<body>' . $html;
2222
}
2323

2424
// parse these elements as void
2525
$html = preg_replace('#<(keygen|source|track|wbr)(?=\s|>)((?:"[^"]*"|\'[^\']*\'|[^"\'>])*+)(?<!/)>#', '<$1$2 />', $html);
2626

2727
// fix parsing of </ inside scripts
28-
$html = preg_replace_callback('#(<script(?=\s|>)(?:"[^"]*"|\'[^\']*\'|[^"\'>])*+>)(.*?)(</script>)#s', function (array $m): string {
29-
return $m[1] . str_replace('</', '<\/', $m[2]) . $m[3];
30-
}, $html);
28+
$html = preg_replace_callback(
29+
'#(<script(?=\s|>)(?:"[^"]*"|\'[^\']*\'|[^"\'>])*+>)(.*?)(</script>)#s',
30+
fn(array $m): string => $m[1] . str_replace('</', '<\/', $m[2]) . $m[3],
31+
$html
32+
);
3133

3234
$dom = new \DOMDocument;
3335
$old = libxml_use_internal_errors(true);

src/Framework/Dumper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static function toLine($var): string
7272
return "[$out]";
7373

7474
} elseif ($var instanceof \Throwable) {
75-
return 'Exception ' . get_class($var) . ': ' . ($var->getCode() ? '#' . $var->getCode() . ' ' : '') . $var->getMessage();
75+
return 'Exception ' . $var::class . ': ' . ($var->getCode() ? '#' . $var->getCode() . ' ' : '') . $var->getMessage();
7676

7777
} elseif ($var instanceof Expect) {
7878
return $var->dump();
@@ -95,7 +95,7 @@ public static function toLine($var): string
9595
*/
9696
private static function objectToLine($object): string
9797
{
98-
$line = get_class($object);
98+
$line = $object::class;
9999
if ($object instanceof \DateTime || $object instanceof \DateTimeInterface) {
100100
$line .= '(' . $object->format('Y-m-d H:i:s O') . ')';
101101
}
@@ -128,7 +128,7 @@ private static function _toPhp(&$var, array &$list = [], int $level = 0, int &$l
128128
{
129129
if (is_float($var)) {
130130
$var = str_replace(',', '.', "$var");
131-
return strpos($var, '.') === false ? $var . '.0' : $var;
131+
return !str_contains($var, '.') ? $var . '.0' : $var;
132132

133133
} elseif (is_bool($var)) {
134134
return $var ? 'true' : 'false';
@@ -173,7 +173,7 @@ private static function _toPhp(&$var, array &$list = [], int $level = 0, int &$l
173173
}
174174

175175
unset($var[$marker]);
176-
if (strpos($outShort, "\n") === false && strlen($outShort) < self::$maxLength) {
176+
if (!str_contains($outShort, "\n") && strlen($outShort) < self::$maxLength) {
177177
$line = $oldLine;
178178
$out = $outShort;
179179
}
@@ -192,7 +192,7 @@ private static function _toPhp(&$var, array &$list = [], int $level = 0, int &$l
192192

193193
$arr = (array) $var;
194194
$space = str_repeat("\t", $level);
195-
$class = get_class($var);
195+
$class = $var::class;
196196
$used = &$list[spl_object_hash($var)];
197197

198198
if (empty($arr)) {
@@ -356,7 +356,7 @@ public static function dumpException(\Throwable $e): string
356356
'%2' => self::color('yellow') . self::toLine($expected) . self::color('white'),
357357
]);
358358
} else {
359-
$message = ($e instanceof \ErrorException ? Helpers::errorTypeToString($e->getSeverity()) : get_class($e))
359+
$message = ($e instanceof \ErrorException ? Helpers::errorTypeToString($e->getSeverity()) : $e::class)
360360
. ': ' . preg_replace('#[\x00-\x09\x0B-\x1F]+#', ' ', $e->getMessage());
361361
}
362362

src/Framework/Environment.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ public static function setupColors(): void
8585
: @stream_isatty(STDOUT)) // @ may trigger error 'cannot cast a filtered stream on this system'
8686
);
8787

88-
ob_start(function (string $s): string {
89-
return self::$useColors ? $s : Dumper::removeColors($s);
90-
}, 1, PHP_OUTPUT_HANDLER_FLUSHABLE);
88+
ob_start(
89+
fn(string $s): string => self::$useColors ? $s : Dumper::removeColors($s),
90+
1,
91+
PHP_OUTPUT_HANDLER_FLUSHABLE
92+
);
9193
}
9294

9395

@@ -191,7 +193,7 @@ public static function getTestAnnotations(): array
191193
public static function bypassFinals(): void
192194
{
193195
FileMutator::addMutator(function (string $code): string {
194-
if (strpos($code, 'final') !== false) {
196+
if (str_contains($code, 'final')) {
195197
$tokens = token_get_all($code, TOKEN_PARSE);
196198
$code = '';
197199
foreach ($tokens as $token) {

src/Framework/TestCase.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public function run(): void
3636
throw new \LogicException('Calling TestCase::run($method) is deprecated. Use TestCase::runTest($method) instead.');
3737
}
3838

39-
$methods = array_values(preg_grep(self::MethodPattern, array_map(function (\ReflectionMethod $rm): string {
40-
return $rm->getName();
41-
}, (new \ReflectionObject($this))->getMethods())));
39+
$methods = array_values(preg_grep(
40+
self::MethodPattern,
41+
array_map(fn(\ReflectionMethod $rm): string => $rm->getName(), (new \ReflectionObject($this))->getMethods())
42+
));
4243

4344
if (isset($_SERVER['argv']) && ($tmp = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv']))) {
4445
$method = reset($tmp);
@@ -153,7 +154,7 @@ public function runTest(string $method, ?array $args = null): void
153154
*/
154155
protected function getData(string $provider)
155156
{
156-
if (strpos($provider, '.') === false) {
157+
if (!str_contains($provider, '.')) {
157158
return $this->$provider();
158159
} else {
159160
$rc = new \ReflectionClass($this);
@@ -247,7 +248,7 @@ private function prepareTestData(\ReflectionMethod $method, array $dataprovider)
247248

248249
foreach ($res as $k => $set) {
249250
if (!is_array($set)) {
250-
$type = is_object($set) ? get_class($set) : gettype($set);
251+
$type = is_object($set) ? $set::class : gettype($set);
251252
throw new TestCaseException("Data provider $provider() item '$k' must be an array, $type given.");
252253
}
253254

src/Runner/CliTester.php

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -98,64 +98,66 @@ private function loadOptions(): CommandLine
9898
$outputFiles = [];
9999

100100
echo <<<'XX'
101-
_____ ___ ___ _____ ___ ___
102-
|_ _/ __)( __/_ _/ __)| _ )
103-
|_| \___ /___) |_| \___ |_|_\ v2.4.3
104-
105-
106-
XX;
107-
108-
$cmd = new CommandLine(<<<'XX'
109-
Usage:
110-
tester [options] [<test file> | <directory>]...
111-
112-
Options:
113-
-p <path> Specify PHP interpreter to run (default: php).
114-
-c <path> Look for php.ini file (or look in directory) <path>.
115-
-C Use system-wide php.ini.
116-
-d <key=value>... Define INI entry 'key' with value 'value'.
117-
-s Show information about skipped tests.
118-
--stop-on-fail Stop execution upon the first failure.
119-
-j <num> Run <num> jobs in parallel (default: 8).
120-
-o <console|tap|junit|log|none> (e.g. -o junit:output.xml)
121-
Specify one or more output formats with optional file name.
122-
-w | --watch <path> Watch directory.
123-
-i | --info Show tests environment info and exit.
124-
--setup <path> Script for runner setup.
125-
--temp <path> Path to temporary directory. Default by sys_get_temp_dir().
126-
--colors [1|0] Enable or disable colors.
127-
--coverage <path> Generate code coverage report to file.
128-
--coverage-src <path> Path to source code.
129-
-h | --help This help.
130-
131-
XX
132-
, [
133-
'-c' => [CommandLine::Realpath => true],
134-
'--watch' => [CommandLine::Repeatable => true, CommandLine::Realpath => true],
135-
'--setup' => [CommandLine::Realpath => true],
136-
'--temp' => [CommandLine::Realpath => true],
137-
'paths' => [CommandLine::Repeatable => true, CommandLine::Value => getcwd()],
138-
'--debug' => [],
139-
'--cider' => [],
140-
'--coverage-src' => [CommandLine::Realpath => true, CommandLine::Repeatable => true],
141-
'-o' => [CommandLine::Repeatable => true, CommandLine::Normalizer => function ($arg) use (&$outputFiles) {
142-
[$format, $file] = explode(':', $arg, 2) + [1 => null];
143-
144-
if (isset($outputFiles[$file])) {
145-
throw new \Exception(
146-
$file === null
147-
? 'Option -o <format> without file name parameter can be used only once.'
148-
: "Cannot specify output by -o into file '$file' more then once."
149-
);
150-
} elseif ($file === null) {
151-
$this->stdoutFormat = $format;
152-
}
101+
_____ ___ ___ _____ ___ ___
102+
|_ _/ __)( __/_ _/ __)| _ )
103+
|_| \___ /___) |_| \___ |_|_\ v2.4.3
104+
105+
106+
XX;
107+
108+
$cmd = new CommandLine(
109+
<<<'XX'
110+
Usage:
111+
tester [options] [<test file> | <directory>]...
112+
113+
Options:
114+
-p <path> Specify PHP interpreter to run (default: php).
115+
-c <path> Look for php.ini file (or look in directory) <path>.
116+
-C Use system-wide php.ini.
117+
-d <key=value>... Define INI entry 'key' with value 'value'.
118+
-s Show information about skipped tests.
119+
--stop-on-fail Stop execution upon the first failure.
120+
-j <num> Run <num> jobs in parallel (default: 8).
121+
-o <console|tap|junit|log|none> (e.g. -o junit:output.xml)
122+
Specify one or more output formats with optional file name.
123+
-w | --watch <path> Watch directory.
124+
-i | --info Show tests environment info and exit.
125+
--setup <path> Script for runner setup.
126+
--temp <path> Path to temporary directory. Default by sys_get_temp_dir().
127+
--colors [1|0] Enable or disable colors.
128+
--coverage <path> Generate code coverage report to file.
129+
--coverage-src <path> Path to source code.
130+
-h | --help This help.
131+
132+
XX,
133+
[
134+
'-c' => [CommandLine::Realpath => true],
135+
'--watch' => [CommandLine::Repeatable => true, CommandLine::Realpath => true],
136+
'--setup' => [CommandLine::Realpath => true],
137+
'--temp' => [CommandLine::Realpath => true],
138+
'paths' => [CommandLine::Repeatable => true, CommandLine::Value => getcwd()],
139+
'--debug' => [],
140+
'--cider' => [],
141+
'--coverage-src' => [CommandLine::Realpath => true, CommandLine::Repeatable => true],
142+
'-o' => [CommandLine::Repeatable => true, CommandLine::Normalizer => function ($arg) use (&$outputFiles) {
143+
[$format, $file] = explode(':', $arg, 2) + [1 => null];
144+
145+
if (isset($outputFiles[$file])) {
146+
throw new \Exception(
147+
$file === null
148+
? 'Option -o <format> without file name parameter can be used only once.'
149+
: "Cannot specify output by -o into file '$file' more then once."
150+
);
151+
} elseif ($file === null) {
152+
$this->stdoutFormat = $format;
153+
}
153154

154-
$outputFiles[$file] = true;
155+
$outputFiles[$file] = true;
155156

156-
return [$format, $file];
157-
}],
158-
]);
157+
return [$format, $file];
158+
}],
159+
]
160+
);
159161

160162
if (isset($_SERVER['argv'])) {
161163
if (($tmp = array_search('-l', $_SERVER['argv'], true))

src/Runner/Output/ConsolePrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function prepare(Test $test): void
8585
{
8686
if ($this->baseDir === null) {
8787
$this->baseDir = dirname($test->getFile()) . DIRECTORY_SEPARATOR;
88-
} elseif (strpos($test->getFile(), $this->baseDir) !== 0) {
88+
} elseif (!str_starts_with($test->getFile(), $this->baseDir)) {
8989
$common = array_intersect_assoc(
9090
explode(DIRECTORY_SEPARATOR, $this->baseDir),
9191
explode(DIRECTORY_SEPARATOR, $test->getFile())

0 commit comments

Comments
 (0)