Skip to content

Commit 0ca4628

Browse files
author
mmaas
committed
Added snif for print_r
1 parent d922eda commit 0ca4628

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Moxio\CodeSniffer\MoxioSniffs\Sniffs\PHP;
5+
6+
7+
use Moxio\CodeSniffer\MoxioSniffs\Sniffs\AbstractFunctionCallSniff;
8+
use PHP_CodeSniffer\Files\File;
9+
10+
final class DisallowPrintrWithoutWrapperSniff extends AbstractFunctionCallSniff
11+
{
12+
13+
protected function registerFunctions()
14+
{
15+
return ['print_r'];
16+
}
17+
18+
protected function processFunctionCall(File $phpcsFile, $functionName, $functionNamePtr, $argumentPtrs)
19+
{
20+
if (count($argumentPtrs) < 2) {
21+
$error = "Do not commit code that prints directly to the console. Add 'true' or use '\MXO_Debug_Helper::print_r()'";
22+
$phpcsFile->addError($error, $functionNamePtr, 'Disallowed');
23+
}
24+
}
25+
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
print_r('foo');
3+
print_r(['a']);
4+
print_r(['a' => 1, 'b' => 2]);
5+
print_r(['a'], true);
6+
print_r(['a' => 1, 'b' => 2], true);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace Moxio\CodeSniffer\MoxioSniffs\Sniffs\Tests\PHP;
3+
4+
use Moxio\CodeSniffer\MoxioSniffs\Sniffs\PHP\DisallowPrintrWithoutWrapperSniff;
5+
use Moxio\CodeSniffer\MoxioSniffs\Sniffs\Tests\AbstractSniffTest;
6+
7+
class DisallowPrintrWithoutWrapperSniffTest extends AbstractSniffTest
8+
{
9+
protected function getSniffClass()
10+
{
11+
return DisallowPrintrWithoutWrapperSniff::class;
12+
}
13+
14+
public function testSniff()
15+
{
16+
$file = __DIR__ . '/DisallowPrintrWithoutWrapperSniffTest.inc';
17+
$this->assertFileHasErrorsOnLines($file, [
18+
2,
19+
3,
20+
4,
21+
]);
22+
}
23+
}

0 commit comments

Comments
 (0)