Skip to content

Commit 2058330

Browse files
We cannot validate deprecation triggers when we load the XML configuration file
1 parent 0cffd28 commit 2058330

File tree

5 files changed

+54
-51
lines changed

5 files changed

+54
-51
lines changed

src/TextUI/Application.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
namespace PHPUnit\TextUI;
1111

1212
use const PHP_EOL;
13+
use function class_exists;
14+
use function explode;
15+
use function function_exists;
1316
use function is_readable;
17+
use function method_exists;
1418
use function printf;
1519
use function realpath;
1620
use function sprintf;
21+
use function str_contains;
1722
use function trim;
1823
use function unlink;
1924
use PHPUnit\Event\EventFacadeIsSealedException;
@@ -182,6 +187,8 @@ public function run(array $argv): int
182187

183188
$baselineGenerator = $this->configureBaseline($configuration);
184189

190+
$this->validateDeprecationTriggers($configuration);
191+
185192
EventFacade::instance()->seal();
186193

187194
$timer = new Timer;
@@ -694,4 +701,43 @@ private function filteredTests(Configuration $configuration, TestSuite $suite):
694701

695702
return $suite->collect();
696703
}
704+
705+
private function validateDeprecationTriggers(Configuration $configuration): void
706+
{
707+
foreach ($configuration->source()->deprecationTriggers()['functions'] as $function) {
708+
if (!function_exists($function)) {
709+
EventFacade::emitter()->testRunnerTriggeredWarning(
710+
sprintf(
711+
'Function %s cannot be configured as a deprecation trigger because it is not declared',
712+
$function,
713+
),
714+
);
715+
}
716+
}
717+
718+
foreach ($configuration->source()->deprecationTriggers()['methods'] as $method) {
719+
if (!str_contains($method, '::')) {
720+
EventFacade::emitter()->testRunnerTriggeredWarning(
721+
sprintf(
722+
'%s cannot be configured as a deprecation trigger because it is not in ClassName::methodName format',
723+
$method,
724+
),
725+
);
726+
727+
continue;
728+
}
729+
730+
[$className, $methodName] = explode('::', $method);
731+
732+
if (!class_exists($className) || !method_exists($className, $methodName)) {
733+
EventFacade::emitter()->testRunnerTriggeredWarning(
734+
sprintf(
735+
'Method %s::%s cannot be configured as a deprecation trigger because it is not declared',
736+
$className,
737+
$methodName,
738+
),
739+
);
740+
}
741+
}
742+
}
697743
}

src/TextUI/Configuration/Value/Source.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
private bool $ignoreSuppressionOfPhpWarnings;
3838

3939
/**
40-
* @psalm-var array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>}
40+
* @psalm-var array{functions: list<non-empty-string>, methods: list<non-empty-string>}
4141
*/
4242
private array $deprecationTriggers;
4343

4444
/**
4545
* @psalm-param non-empty-string $baseline
46-
* @psalm-param array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>} $deprecationTriggers
46+
* @psalm-param array{functions: list<non-empty-string>, methods: list<non-empty-string>} $deprecationTriggers
4747
*/
4848
public function __construct(?string $baseline, bool $ignoreBaseline, FilterDirectoryCollection $includeDirectories, FileCollection $includeFiles, FilterDirectoryCollection $excludeDirectories, FileCollection $excludeFiles, bool $restrictDeprecations, bool $restrictNotices, bool $restrictWarnings, bool $ignoreSuppressionOfDeprecations, bool $ignoreSuppressionOfPhpDeprecations, bool $ignoreSuppressionOfErrors, bool $ignoreSuppressionOfNotices, bool $ignoreSuppressionOfPhpNotices, bool $ignoreSuppressionOfWarnings, bool $ignoreSuppressionOfPhpWarnings, array $deprecationTriggers)
4949
{
@@ -172,7 +172,7 @@ public function ignoreSuppressionOfPhpWarnings(): bool
172172
}
173173

174174
/**
175-
* @psalm-return array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>}
175+
* @psalm-return array{functions: list<non-empty-string>, methods: list<non-empty-string>}
176176
*/
177177
public function deprecationTriggers(): array
178178
{

src/TextUI/Configuration/Xml/LoadedFromFileConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
private ValidationResult $validationResult;
3131

3232
/**
33-
* @param non-empty-string $filename
33+
* @param non-empty-string $filename
3434
*/
3535
public function __construct(string $filename, ValidationResult $validationResult, ExtensionBootstrapCollection $extensions, Source $source, CodeCoverage $codeCoverage, Groups $groups, Logging $logging, Php $php, PHPUnit $phpunit, TestSuiteCollection $testSuite)
3636
{

src/TextUI/Configuration/Xml/Loader.php

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
use const DIRECTORY_SEPARATOR;
1313
use const PHP_VERSION;
1414
use function assert;
15-
use function class_exists;
1615
use function defined;
1716
use function dirname;
1817
use function explode;
19-
use function function_exists;
2018
use function is_numeric;
21-
use function method_exists;
2219
use function preg_match;
2320
use function realpath;
24-
use function sprintf;
2521
use function str_contains;
2622
use function str_starts_with;
2723
use function strlen;
@@ -118,7 +114,7 @@ public function load(string $filename): LoadedFromFileConfiguration
118114
$configurationFileRealpath,
119115
(new Validator)->validate($document, $xsdFilename),
120116
$this->extensions($xpath),
121-
$this->source($configurationFileRealpath, $xpath, $warnings),
117+
$this->source($configurationFileRealpath, $xpath),
122118
$this->codeCoverage($configurationFileRealpath, $xpath),
123119
$this->groups($xpath),
124120
$this->logging($configurationFileRealpath, $xpath),
@@ -250,10 +246,7 @@ private function toAbsolutePath(string $filename, string $path): string
250246
return dirname($filename) . DIRECTORY_SEPARATOR . $path;
251247
}
252248

253-
/**
254-
* @psalm-param list<non-empty-string> $warnings
255-
*/
256-
private function source(string $filename, DOMXPath $xpath, array &$warnings): Source
249+
private function source(string $filename, DOMXPath $xpath): Source
257250
{
258251
$baseline = null;
259252
$restrictDeprecations = false;
@@ -296,46 +289,13 @@ private function source(string $filename, DOMXPath $xpath, array &$warnings): So
296289
foreach ($xpath->query('source/deprecationTrigger/function') as $functionNode) {
297290
assert($functionNode instanceof DOMElement);
298291

299-
if (!function_exists($functionNode->textContent)) {
300-
$warnings[] = sprintf(
301-
'Function %s cannot be configured as a deprecation trigger because it is not declared',
302-
$functionNode->textContent,
303-
);
304-
305-
continue;
306-
}
307-
308292
$deprecationTriggers['functions'][] = $functionNode->textContent;
309293
}
310294

311295
foreach ($xpath->query('source/deprecationTrigger/method') as $methodNode) {
312296
assert($methodNode instanceof DOMElement);
313297

314-
if (!str_contains($methodNode->textContent, '::')) {
315-
$warnings[] = sprintf(
316-
'%s cannot be configured as a deprecation trigger because it is not in ClassName::methodName format',
317-
$methodNode->textContent,
318-
);
319-
320-
continue;
321-
}
322-
323-
[$className, $methodName] = explode('::', $methodNode->textContent);
324-
325-
if (!class_exists($className) || !method_exists($className, $methodName)) {
326-
$warnings[] = sprintf(
327-
'Method %s::%s cannot be configured as a deprecation trigger because it is not declared',
328-
$className,
329-
$methodName,
330-
);
331-
332-
continue;
333-
}
334-
335-
$deprecationTriggers['methods'][] = [
336-
'className' => $className,
337-
'methodName' => $methodName,
338-
];
298+
$deprecationTriggers['methods'][] = $methodNode->textContent;
339299
}
340300

341301
return new Source(

tests/unit/TextUI/Configuration/Xml/LoaderTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ public function testSourceConfigurationIsReadCorrectly(): void
170170
'PHPUnit\TestFixture\DeprecationTrigger\trigger_deprecation',
171171
],
172172
'methods' => [
173-
[
174-
'className' => 'PHPUnit\TestFixture\DeprecationTrigger\DeprecationTrigger',
175-
'methodName' => 'triggerDeprecation',
176-
],
173+
'PHPUnit\TestFixture\DeprecationTrigger\DeprecationTrigger::triggerDeprecation',
177174
],
178175
],
179176
$source->deprecationTriggers(),

0 commit comments

Comments
 (0)