Skip to content

Commit 2ebcaed

Browse files
authored
Merge pull request #889 from PHPCSStandards/feature/generators-show-deprecation-for-methods
Generators: show deprecation notice for deprecated methods
2 parents 69430c5 + a72d2b5 commit 2ebcaed

File tree

7 files changed

+239
-0
lines changed

7 files changed

+239
-0
lines changed

src/Generators/HTML.php

+25
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ public function generate()
164164
*/
165165
protected function printHeader()
166166
{
167+
trigger_error(
168+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedHeader()" instead.',
169+
E_USER_DEPRECATED
170+
);
171+
167172
echo $this->getFormattedHeader();
168173

169174
}//end printHeader()
@@ -208,6 +213,11 @@ protected function getFormattedHeader()
208213
*/
209214
protected function printToc()
210215
{
216+
trigger_error(
217+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedToc()" instead.',
218+
E_USER_DEPRECATED
219+
);
220+
211221
echo $this->getFormattedToc();
212222

213223
}//end printToc()
@@ -260,6 +270,11 @@ protected function getFormattedToc()
260270
*/
261271
protected function printFooter()
262272
{
273+
trigger_error(
274+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedFooter()" instead.',
275+
E_USER_DEPRECATED
276+
);
277+
263278
echo $this->getFormattedFooter();
264279

265280
}//end printFooter()
@@ -367,6 +382,11 @@ private function titleToAnchor($title)
367382
*/
368383
protected function printTextBlock(DOMNode $node)
369384
{
385+
trigger_error(
386+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedTextBlock()" instead.',
387+
E_USER_DEPRECATED
388+
);
389+
370390
echo $this->getFormattedTextBlock($node);
371391

372392
}//end printTextBlock()
@@ -437,6 +457,11 @@ protected function getFormattedTextBlock(DOMNode $node)
437457
*/
438458
protected function printCodeComparisonBlock(DOMNode $node)
439459
{
460+
trigger_error(
461+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedCodeComparisonBlock()" instead.',
462+
E_USER_DEPRECATED
463+
);
464+
440465
echo $this->getFormattedCodeComparisonBlock($node);
441466

442467
}//end printCodeComparisonBlock()

src/Generators/Markdown.php

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public function generate()
5757
*/
5858
protected function printHeader()
5959
{
60+
trigger_error(
61+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedHeader()" instead.',
62+
E_USER_DEPRECATED
63+
);
64+
6065
echo $this->getFormattedHeader();
6166

6267
}//end printHeader()
@@ -89,6 +94,11 @@ protected function getFormattedHeader()
8994
*/
9095
protected function printFooter()
9196
{
97+
trigger_error(
98+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedFooter()" instead.',
99+
E_USER_DEPRECATED
100+
);
101+
92102
echo $this->getFormattedFooter();
93103

94104
}//end printFooter()
@@ -157,6 +167,11 @@ protected function processSniff(DOMNode $doc)
157167
*/
158168
protected function printTextBlock(DOMNode $node)
159169
{
170+
trigger_error(
171+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedTextBlock()" instead.',
172+
E_USER_DEPRECATED
173+
);
174+
160175
echo $this->getFormattedTextBlock($node);
161176

162177
}//end printTextBlock()
@@ -225,6 +240,11 @@ protected function getFormattedTextBlock(DOMNode $node)
225240
*/
226241
protected function printCodeComparisonBlock(DOMNode $node)
227242
{
243+
trigger_error(
244+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedCodeComparisonBlock()" instead.',
245+
E_USER_DEPRECATED
246+
);
247+
228248
echo $this->getFormattedCodeComparisonBlock($node);
229249

230250
}//end printCodeComparisonBlock()

src/Generators/Text.php

+15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function processSniff(DOMNode $doc)
6262
*/
6363
protected function printTitle(DOMNode $doc)
6464
{
65+
trigger_error(
66+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedTitle()" instead.',
67+
E_USER_DEPRECATED
68+
);
69+
6570
echo $this->getFormattedTitle($doc);
6671

6772
}//end printTitle()
@@ -109,6 +114,11 @@ protected function getFormattedTitle(DOMNode $doc)
109114
*/
110115
protected function printTextBlock(DOMNode $node)
111116
{
117+
trigger_error(
118+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedTextBlock()" instead.',
119+
E_USER_DEPRECATED
120+
);
121+
112122
echo $this->getFormattedTextBlock($node);
113123

114124
}//end printTextBlock()
@@ -155,6 +165,11 @@ protected function getFormattedTextBlock(DOMNode $node)
155165
*/
156166
protected function printCodeComparisonBlock(DOMNode $node)
157167
{
168+
trigger_error(
169+
'The '.__METHOD__.'() method is deprecated. Use "echo '.__CLASS__.'::getFormattedCodeComparisonBlock()" instead.',
170+
E_USER_DEPRECATED
171+
);
172+
158173
echo $this->getFormattedCodeComparisonBlock($node);
159174

160175
}//end printCodeComparisonBlock()

tests/Core/Generators/Fixtures/HTMLDouble.php

+30
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,34 @@ public function getRealFooter()
3737
{
3838
return parent::getFormattedFooter();
3939
}
40+
41+
/**
42+
* [VISIBILITY WIDENING ONLY] Print the header of the HTML page.
43+
*
44+
* @return void
45+
*/
46+
public function printHeader()
47+
{
48+
parent::printHeader();
49+
}
50+
51+
/**
52+
* [VISIBILITY WIDENING ONLY] Print the table of contents for the standard.
53+
*
54+
* @return void
55+
*/
56+
public function printToc()
57+
{
58+
parent::printToc();
59+
}
60+
61+
/**
62+
* [VISIBILITY WIDENING ONLY] Print the footer of the HTML page.
63+
*
64+
* @return void
65+
*/
66+
public function printFooter()
67+
{
68+
parent::printFooter();
69+
}
4070
}

tests/Core/Generators/Fixtures/MarkdownDouble.php

+30
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,34 @@ public function getRealFooter()
3535
{
3636
return parent::getFormattedFooter();
3737
}
38+
39+
/**
40+
* [VISIBILITY WIDENING ONLY] Print the header of the HTML page.
41+
*
42+
* @return void
43+
*/
44+
public function printHeader()
45+
{
46+
parent::printHeader();
47+
}
48+
49+
/**
50+
* [VISIBILITY WIDENING ONLY] Print the table of contents for the standard.
51+
*
52+
* @return void
53+
*/
54+
public function printToc()
55+
{
56+
parent::printToc();
57+
}
58+
59+
/**
60+
* [VISIBILITY WIDENING ONLY] Print the footer of the HTML page.
61+
*
62+
* @return void
63+
*/
64+
public function printFooter()
65+
{
66+
parent::printFooter();
67+
}
3868
}

tests/Core/Generators/HTMLTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,64 @@ public function testFooterDoesntThrowWarningOnMissingTimezone()
363363
}//end testFooterDoesntThrowWarningOnMissingTimezone()
364364

365365

366+
/**
367+
* Perfunctory test to verify that extenders which call deprecated methods will see a deprecation notice.
368+
*
369+
* Note: not all deprecated methods are tested as some need arguments.
370+
*
371+
* @param string $methodName Name of the deprecated method to test.
372+
*
373+
* @dataProvider dataCallingDeprecatedMethodThrowsDeprecationNotice
374+
*
375+
* @return void
376+
*/
377+
public function testCallingDeprecatedMethodThrowsDeprecationNotice($methodName)
378+
{
379+
$exceptionClass = 'PHPUnit\Framework\Error\Deprecated';
380+
if (class_exists($exceptionClass) === false) {
381+
$exceptionClass = 'PHPUnit_Framework_Error_Deprecated';
382+
}
383+
384+
$regex = '`^The PHP_CodeSniffer\\\\Generators\\\\HTML::%s\(\) method is deprecated\. Use "echo [^\s]+::%s\(\)" instead\.$`';
385+
$regex = sprintf($regex, preg_quote($methodName, '`'), str_replace('print', 'getFormatted', $methodName));
386+
387+
if (method_exists($this, 'expectExceptionMessageMatches') === true) {
388+
$this->expectException($exceptionClass);
389+
$this->expectExceptionMessageMatches($regex);
390+
} else if (method_exists($this, 'expectExceptionMessageRegExp') === true) {
391+
// PHPUnit < 8.4.0.
392+
$this->expectException($exceptionClass);
393+
$this->expectExceptionMessageRegExp($regex);
394+
} else {
395+
// PHPUnit < 5.2.0.
396+
$this->setExpectedExceptionRegExp($exceptionClass, $regex);
397+
}
398+
399+
// Set up the ruleset.
400+
$standard = __DIR__.'/OneDocTest.xml';
401+
$config = new ConfigDouble(["--standard=$standard"]);
402+
$ruleset = new Ruleset($config);
403+
404+
$generator = new HTMLDouble($ruleset);
405+
$generator->$methodName();
406+
407+
}//end testCallingDeprecatedMethodThrowsDeprecationNotice()
408+
409+
410+
/**
411+
* Data provider.
412+
*
413+
* @return array<string, array<string, string>>
414+
*/
415+
public static function dataCallingDeprecatedMethodThrowsDeprecationNotice()
416+
{
417+
return [
418+
'printHeader()' => ['printHeader'],
419+
'printToc()' => ['printToc'],
420+
'printFooter()' => ['printFooter'],
421+
];
422+
423+
}//end dataCallingDeprecatedMethodThrowsDeprecationNotice()
424+
425+
366426
}//end class

tests/Core/Generators/MarkdownTest.php

+59
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,63 @@ public function testFooterDoesntThrowWarningOnMissingTimezone()
335335
}//end testFooterDoesntThrowWarningOnMissingTimezone()
336336

337337

338+
/**
339+
* Perfunctory test to verify that extenders which call deprecated methods will see a deprecation notice.
340+
*
341+
* Note: not all deprecated methods are tested as some need arguments.
342+
*
343+
* @param string $methodName Name of the deprecated method to test.
344+
*
345+
* @dataProvider dataCallingDeprecatedMethodThrowsDeprecationNotice
346+
*
347+
* @return void
348+
*/
349+
public function testCallingDeprecatedMethodThrowsDeprecationNotice($methodName)
350+
{
351+
$exceptionClass = 'PHPUnit\Framework\Error\Deprecated';
352+
if (class_exists($exceptionClass) === false) {
353+
$exceptionClass = 'PHPUnit_Framework_Error_Deprecated';
354+
}
355+
356+
$regex = '`^The PHP_CodeSniffer\\\\Generators\\\\Markdown::%s\(\) method is deprecated\. Use "echo [^\s]+::%s\(\)" instead\.$`';
357+
$regex = sprintf($regex, preg_quote($methodName, '`'), str_replace('print', 'getFormatted', $methodName));
358+
359+
if (method_exists($this, 'expectExceptionMessageMatches') === true) {
360+
$this->expectException($exceptionClass);
361+
$this->expectExceptionMessageMatches($regex);
362+
} else if (method_exists($this, 'expectExceptionMessageRegExp') === true) {
363+
// PHPUnit < 8.4.0.
364+
$this->expectException($exceptionClass);
365+
$this->expectExceptionMessageRegExp($regex);
366+
} else {
367+
// PHPUnit < 5.2.0.
368+
$this->setExpectedExceptionRegExp($exceptionClass, $regex);
369+
}
370+
371+
// Set up the ruleset.
372+
$standard = __DIR__.'/OneDocTest.xml';
373+
$config = new ConfigDouble(["--standard=$standard"]);
374+
$ruleset = new Ruleset($config);
375+
376+
$generator = new MarkdownDouble($ruleset);
377+
$generator->$methodName();
378+
379+
}//end testCallingDeprecatedMethodThrowsDeprecationNotice()
380+
381+
382+
/**
383+
* Data provider.
384+
*
385+
* @return array<string, array<string, string>>
386+
*/
387+
public static function dataCallingDeprecatedMethodThrowsDeprecationNotice()
388+
{
389+
return [
390+
'printHeader()' => ['printHeader'],
391+
'printFooter()' => ['printFooter'],
392+
];
393+
394+
}//end dataCallingDeprecatedMethodThrowsDeprecationNotice()
395+
396+
338397
}//end class

0 commit comments

Comments
 (0)