Skip to content

Commit 027b14a

Browse files
Initial work on configuring deprecation triggers
1 parent 16bcac0 commit 027b14a

File tree

17 files changed

+373
-3
lines changed

17 files changed

+373
-3
lines changed

.psalm/baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@
895895
<file src="src/TextUI/Configuration/Xml/Loader.php">
896896
<ArgumentTypeCoercion>
897897
<code><![CDATA[$bootstrap->getAttribute('class')]]></code>
898+
<code><![CDATA[$deprecationTriggers]]></code>
898899
<code><![CDATA[$directoryNode->getAttribute('phpVersionOperator')]]></code>
899900
<code><![CDATA[$fileNode->getAttribute('phpVersionOperator')]]></code>
900901
</ArgumentTypeCoercion>

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"tests/"
7575
],
7676
"files": [
77+
"tests/_files/deprecation-trigger/trigger_deprecation.php",
7778
"tests/_files/CoverageNamespacedFunctionTest.php",
7879
"tests/_files/CoveredFunction.php",
7980
"tests/_files/Generator.php",

phpunit.xsd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<xs:group ref="sourcePathGroup"/>
2424
</xs:complexType>
2525
</xs:element>
26+
<xs:element name="deprecationTrigger" type="deprecationTriggerType" minOccurs="0"/>
2627
</xs:all>
2728
<xs:attribute name="baseline" type="xs:anyURI"/>
2829
<xs:attribute name="restrictDeprecations" type="xs:boolean" default="false"/>
@@ -320,4 +321,12 @@
320321
<xs:attribute name="showUncoveredFiles" type="xs:boolean" default="false"/>
321322
<xs:attribute name="showOnlySummary" type="xs:boolean" default="false"/>
322323
</xs:complexType>
324+
<xs:complexType name="deprecationTriggerType">
325+
<xs:sequence>
326+
<xs:choice maxOccurs="unbounded">
327+
<xs:element name="function" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
328+
<xs:element name="method" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
329+
</xs:choice>
330+
</xs:sequence>
331+
</xs:complexType>
323332
</xs:schema>

src/TextUI/Configuration/Merger.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ public function merge(CliConfiguration $cliConfiguration, XmlConfiguration $xmlC
739739
$xmlConfiguration->source()->ignoreSuppressionOfPhpNotices(),
740740
$xmlConfiguration->source()->ignoreSuppressionOfWarnings(),
741741
$xmlConfiguration->source()->ignoreSuppressionOfPhpWarnings(),
742+
$xmlConfiguration->source()->deprecationTriggers(),
742743
),
743744
$testResultCacheFile,
744745
$coverageClover,

src/TextUI/Configuration/Value/Source.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@
3636
private bool $ignoreSuppressionOfWarnings;
3737
private bool $ignoreSuppressionOfPhpWarnings;
3838

39+
/**
40+
* @psalm-var array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>}
41+
*/
42+
private array $deprecationTriggers;
43+
3944
/**
4045
* @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
4147
*/
42-
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)
48+
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)
4349
{
4450
$this->baseline = $baseline;
4551
$this->ignoreBaseline = $ignoreBaseline;
@@ -57,6 +63,7 @@ public function __construct(?string $baseline, bool $ignoreBaseline, FilterDirec
5763
$this->ignoreSuppressionOfPhpNotices = $ignoreSuppressionOfPhpNotices;
5864
$this->ignoreSuppressionOfWarnings = $ignoreSuppressionOfWarnings;
5965
$this->ignoreSuppressionOfPhpWarnings = $ignoreSuppressionOfPhpWarnings;
66+
$this->deprecationTriggers = $deprecationTriggers;
6067
}
6168

6269
/**
@@ -163,4 +170,12 @@ public function ignoreSuppressionOfPhpWarnings(): bool
163170
{
164171
return $this->ignoreSuppressionOfPhpWarnings;
165172
}
173+
174+
/**
175+
* @psalm-return array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>}
176+
*/
177+
public function deprecationTriggers(): array
178+
{
179+
return $this->deprecationTriggers;
180+
}
166181
}

src/TextUI/Configuration/Xml/DefaultConfiguration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public static function create(): self
5252
false,
5353
false,
5454
false,
55+
[
56+
'functions' => [],
57+
'methods' => [],
58+
],
5559
),
5660
new CodeCoverage(
5761
false,

src/TextUI/Configuration/Xml/Loader.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
use const DIRECTORY_SEPARATOR;
1313
use const PHP_VERSION;
1414
use function assert;
15+
use function class_exists;
1516
use function defined;
1617
use function dirname;
1718
use function explode;
19+
use function function_exists;
1820
use function is_numeric;
21+
use function method_exists;
1922
use function preg_match;
2023
use function realpath;
24+
use function sprintf;
2125
use function str_contains;
2226
use function str_starts_with;
2327
use function strlen;
@@ -116,7 +120,7 @@ public function load(string $filename): LoadedFromFileConfiguration
116120
$configurationFileRealpath,
117121
(new Validator)->validate($document, $xsdFilename),
118122
$this->extensions($xpath),
119-
$this->source($configurationFileRealpath, $xpath),
123+
$this->source($configurationFileRealpath, $xpath, $warnings),
120124
$this->codeCoverage($configurationFileRealpath, $xpath),
121125
$this->groups($xpath),
122126
$this->logging($configurationFileRealpath, $xpath),
@@ -249,7 +253,10 @@ private function toAbsolutePath(string $filename, string $path): string
249253
return dirname($filename) . DIRECTORY_SEPARATOR . $path;
250254
}
251255

252-
private function source(string $filename, DOMXPath $xpath): Source
256+
/**
257+
* @psalm-param list<non-empty-string> $warnings
258+
*/
259+
private function source(string $filename, DOMXPath $xpath, array &$warnings): Source
253260
{
254261
$baseline = null;
255262
$restrictDeprecations = false;
@@ -284,6 +291,56 @@ private function source(string $filename, DOMXPath $xpath): Source
284291
$ignoreSuppressionOfPhpWarnings = $this->getBooleanAttribute($element, 'ignoreSuppressionOfPhpWarnings', false);
285292
}
286293

294+
$deprecationTriggers = [
295+
'functions' => [],
296+
'methods' => [],
297+
];
298+
299+
foreach ($xpath->query('source/deprecationTrigger/function') as $functionNode) {
300+
assert($functionNode instanceof DOMElement);
301+
302+
if (!function_exists($functionNode->textContent)) {
303+
$warnings[] = sprintf(
304+
'Function %s cannot be configured as a deprecation trigger because it is not declared',
305+
$functionNode->textContent,
306+
);
307+
308+
continue;
309+
}
310+
311+
$deprecationTriggers['functions'][] = $functionNode->textContent;
312+
}
313+
314+
foreach ($xpath->query('source/deprecationTrigger/method') as $methodNode) {
315+
assert($methodNode instanceof DOMElement);
316+
317+
if (!str_contains($methodNode->textContent, '::')) {
318+
$warnings[] = sprintf(
319+
'%s cannot be configured as a deprecation trigger because it is not in ClassName::methodName format',
320+
$methodNode->textContent,
321+
);
322+
323+
continue;
324+
}
325+
326+
[$className, $methodName] = explode('::', $methodNode->textContent);
327+
328+
if (!class_exists($className) || !method_exists($className, $methodName)) {
329+
$warnings[] = sprintf(
330+
'Method %s::%s cannot be configured as a deprecation trigger because it is not declared',
331+
$className,
332+
$methodName,
333+
);
334+
335+
continue;
336+
}
337+
338+
$deprecationTriggers['methods'][] = [
339+
'className' => $className,
340+
'methodName' => $methodName,
341+
];
342+
}
343+
287344
return new Source(
288345
$baseline,
289346
false,
@@ -301,6 +358,7 @@ private function source(string $filename, DOMXPath $xpath): Source
301358
$ignoreSuppressionOfPhpNotices,
302359
$ignoreSuppressionOfWarnings,
303360
$ignoreSuppressionOfPhpWarnings,
361+
$deprecationTriggers,
304362
);
305363
}
306364

tests/_files/configuration_codecoverage.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<directory suffix=".php">/path/to/files</directory>
1515
<file>/path/to/file</file>
1616
</exclude>
17+
18+
<deprecationTrigger>
19+
<function>PHPUnit\TestFixture\DeprecationTrigger\trigger_deprecation</function>
20+
<method>PHPUnit\TestFixture\DeprecationTrigger\DeprecationTrigger::triggerDeprecation</method>
21+
</deprecationTrigger>
1722
</source>
1823

1924
<coverage pathCoverage="true"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\DeprecationTrigger;
11+
12+
final class DeprecationTrigger
13+
{
14+
public function triggerDeprecation(): void
15+
{
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\DeprecationTrigger;
11+
12+
function trigger_deprecation(): void
13+
{
14+
}

0 commit comments

Comments
 (0)