Skip to content

Commit c801519

Browse files
committed
Ensure the script threats PHP open tags in a case insensitive way
1 parent 9241537 commit c801519

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

Scripts/DocCodeExamples/XmlDocValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private function displayErrorMessage(string $message)
335335
*/
336336
private function maybeAddPhpOpenTag(string $codeBlockContent): string
337337
{
338-
if (\strpos(\trim($codeBlockContent), '<?php') === false) {
338+
if (\strpos(\trim(\strtolower($codeBlockContent)), '<?php') === false) {
339339
$codeBlockContent = '<?php ' . \PHP_EOL . $codeBlockContent;
340340
}
341341

Tests/DocCodeExamples/XmlDocValidatorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ public static function dataValidate()
212212
'expectedErrorMessage' => '',
213213
'expectedReturnValue' => true,
214214
],
215+
'Code examples testing adding or not the PHP open tag' => [
216+
'xmlPath' => 'Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/Docs/Examples/PhpOpenTagStandard.xml',
217+
'expectedErrorMessage' => '',
218+
'expectedReturnValue' => true,
219+
],
215220
'Code example with syntax error using colorized output' => [
216221
'xmlPath' => 'Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/Docs/Examples/SyntaxErrorExampleStandard.xml',
217222
'expectedErrorMessage' => "\033[31mErrors found while processing Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/Docs/Examples/SyntaxErrorExampleStandard.xml\033[0m" . PHP_EOL . PHP_EOL .
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<documentation title="PHP Open Tag">
2+
<standard>
3+
<![CDATA[
4+
Documentation containing valid and invalid code examples that are correct. Uses a variation of
5+
tests with and without the PHP open tag to test that the script correctly adds one when needed.
6+
]]>
7+
</standard>
8+
<code_comparison>
9+
<code title="Valid: valid code example without PHP open tag.">
10+
<![CDATA[
11+
function sniffValidationWillPass() {}
12+
]]>
13+
</code>
14+
<code title="Invalid: invalid code example with PHP open tag.">
15+
<![CDATA[
16+
<?php
17+
function sniffValidationWillFail() {}
18+
]]>
19+
</code>
20+
</code_comparison>
21+
<code_comparison>
22+
<code title="Valid: valid code example with uppercase PHP open tag.">
23+
<![CDATA[
24+
<?PHP
25+
function sniffValidationWillPass() {}
26+
]]>
27+
</code>
28+
<code title="Invalid: invalid code example with varying case PHP open tag.">
29+
<![CDATA[
30+
<?pHp
31+
function sniffValidationWillFail() {}
32+
]]>
33+
</code>
34+
</code_comparison>
35+
</documentation>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
/**
4+
* Test fixture.
5+
*
6+
* @see \PHPCSDevTools\Tests\DocCodeExamples\XmlDocValidatorTest
7+
*/
8+
9+
namespace Fixtures\DocCodeExamples\CheckCodeExamplesStandard\Sniffs\Examples;
10+
11+
class PhpOpenTagSniff extends BaseExamplesSniff {}

0 commit comments

Comments
 (0)