Skip to content

Commit 43904bd

Browse files
committed
SlevomatCodingStandard.Variables.DisallowVariableVariable: New sniff
1 parent 4698435 commit 43904bd

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Variables;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use const T_DOLLAR;
8+
use const T_DOLLAR_OPEN_CURLY_BRACES;
9+
10+
class DisallowVariableVariableSniff implements Sniff
11+
{
12+
13+
public const CODE_DISALLOWED_VARIABLE_VARIABLE = 'DisallowedVariableVariable';
14+
15+
/**
16+
* @return array<int, (int|string)>
17+
*/
18+
public function register(): array
19+
{
20+
return [
21+
T_DOLLAR,
22+
T_DOLLAR_OPEN_CURLY_BRACES,
23+
];
24+
}
25+
26+
/**
27+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
28+
* @param int $pointer
29+
*/
30+
public function process(File $phpcsFile, $pointer): void
31+
{
32+
$phpcsFile->addError('Use of variable variable is disallowed.', $pointer, self::CODE_DISALLOWED_VARIABLE_VARIABLE);
33+
}
34+
35+
}

doc/variables.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
Disallows use of super global variables.
66

7+
#### SlevomatCodingStandard.Variables.DisallowVariableVariable
8+
9+
Disallows use of variable variables.
10+
711
#### SlevomatCodingStandard.Variables.DuplicateAssignmentToVariable
812

913
Looks for duplicate assignments to a variable.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Variables;
4+
5+
use SlevomatCodingStandard\Sniffs\TestCase;
6+
7+
class DisallowVariableVariableSniffTest extends TestCase
8+
{
9+
10+
public function testNoErrors(): void
11+
{
12+
$report = self::checkFile(__DIR__ . '/data/disallowVariableVariableNoErrors.php');
13+
self::assertNoSniffErrorInFile($report);
14+
}
15+
16+
public function testErrors(): void
17+
{
18+
$report = self::checkFile(__DIR__ . '/data/disallowVariableVariableErrors.php');
19+
20+
self::assertSame(3, $report->getErrorCount());
21+
22+
self::assertSniffError($report, 4, DisallowVariableVariableSniff::CODE_DISALLOWED_VARIABLE_VARIABLE);
23+
self::assertSniffError($report, 13, DisallowVariableVariableSniff::CODE_DISALLOWED_VARIABLE_VARIABLE);
24+
self::assertSniffError($report, 14, DisallowVariableVariableSniff::CODE_DISALLOWED_VARIABLE_VARIABLE);
25+
}
26+
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
$a;
4+
$$a;
5+
6+
class Foo
7+
{
8+
9+
private $foo;
10+
11+
public function setFoo($foo)
12+
{
13+
$this->${'foo'}
14+
= $$foo;
15+
}
16+
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$a;
4+
5+
class Foo
6+
{
7+
8+
private $foo;
9+
10+
public function setFoo($foo)
11+
{
12+
$this->foo = $foo;
13+
}
14+
15+
}

0 commit comments

Comments
 (0)