-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from unleashedtech/forbidden-classes
New Sniff: Forbidden classes
- Loading branch information
Showing
10 changed files
with
161 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Unleashed\Sniffs\PHP; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use SlevomatCodingStandard\Helpers\NamespaceHelper; | ||
use SlevomatCodingStandard\Helpers\ReferencedNameHelper; | ||
use SlevomatCodingStandard\Helpers\TokenHelper; | ||
|
||
final class ForbiddenClassesSniff implements Sniff | ||
{ | ||
public const FORBIDDEN = 'Forbidden'; | ||
|
||
/** | ||
* A list of fully-qualified class, interface, or trait names | ||
* | ||
* @var string[] | ||
*/ | ||
public $forbiddenClasses = [ | ||
// phpcs:disable Unleashed.PHP.ForbiddenClasses.Forbidden | ||
\DateTime::class, | ||
// phpcs:enable | ||
]; | ||
|
||
/** | ||
* If true, an error will be thrown; otherwise a warning | ||
* | ||
* @var bool | ||
*/ | ||
public $error = true; | ||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @inheritDoc | ||
*/ | ||
public function register(): array | ||
{ | ||
return [\T_OPEN_TAG]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
if (TokenHelper::findPrevious($phpcsFile, T_OPEN_TAG, $stackPtr - 1) !== null) { | ||
return; | ||
} | ||
|
||
$referencedNames = ReferencedNameHelper::getAllReferencedNames($phpcsFile, $stackPtr); | ||
|
||
foreach ($referencedNames as $referencedName) { | ||
$pointer = $referencedName->getStartPointer(); | ||
$name = $referencedName->getNameAsReferencedInFile(); | ||
|
||
$fullyQualifiedName = NamespaceHelper::resolveClassName($phpcsFile, $name, $pointer); | ||
|
||
if (! \in_array($fullyQualifiedName, $this->forbiddenClasses, true)) { | ||
continue; | ||
} | ||
|
||
$error = \sprintf('The use of "%s" is forbidden', $fullyQualifiedName); | ||
if ($this->error) { | ||
$phpcsFile->addError($error, $pointer, self::FORBIDDEN); | ||
} else { | ||
$phpcsFile->addWarning($error, $pointer, self::FORBIDDEN); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test; | ||
|
||
use DateTime as Date; | ||
|
||
class ForbiddenClasses extends Date implements Date | ||
{ | ||
use Date; | ||
|
||
public function foo(): void | ||
{ | ||
$x = new Date(); | ||
$y = new \DateTime(); | ||
|
||
if ($x instanceof Date) { | ||
echo Date::ISO8601; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
|
||
$foo = $bar = new stdClass(); | ||
|
||
$baz = $baz = new DateTime(); | ||
$baz = $baz = new DateTimeImmutable(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test; | ||
|
||
use DateTime as Date; | ||
|
||
class ForbiddenClasses extends Date implements Date | ||
{ | ||
use Date; | ||
|
||
public function foo(): void | ||
{ | ||
$x = new Date(); | ||
$y = new \DateTime(); | ||
|
||
if ($x instanceof Date) { | ||
echo Date::ISO8601; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
|
||
$foo = $bar = new stdClass(); | ||
|
||
$baz = $baz = new DateTime(); | ||
$baz = $baz = new DateTimeImmutable(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters