-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisallowGuardedAttributeSniff.php
More file actions
64 lines (50 loc) · 2.02 KB
/
DisallowGuardedAttributeSniff.php
File metadata and controls
64 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php declare(strict_types=1);
namespace IxDFCodingStandard\Sniffs\Laravel;
use IxDFCodingStandard\Helpers\ClassHelper;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
final class DisallowGuardedAttributeSniff extends AbstractScopeSniff
{
public const CODE_EMPTY_GUARDED = 'EmptyGuarded';
public const CODE_NON_EMPTY_GUARDED = 'NonEmptyGuarded';
/**
* A list of tokenizers this sniff supports.
* @var list<string>
*/
public array $supportedTokenizers = ['PHP'];
/** Constructs the test with the tokens it wishes to listen for. */
public function __construct()
{
parent::__construct([\T_CLASS], [\T_VARIABLE], false);
}
/** @inheritDoc */
protected function processTokenWithinScope(File $phpcsFile, $varPointer, $currScope)
{
$varToken = $phpcsFile->getTokens()[$varPointer];
if ($varToken['content'] !== '$guarded') {
return;
}
$classTokenPointer = $phpcsFile->findPrevious([\T_CLASS], $varPointer);
$classFQCN = ClassHelper::getFullyQualifiedName($phpcsFile, $classTokenPointer);
$probablyModelInstance = new $classFQCN(); // @todo find a more performant option
if (! $probablyModelInstance instanceof \Illuminate\Database\Eloquent\Model) {
return;
}
$modelInstance = $probablyModelInstance;
if ($modelInstance->getGuarded() === []) {
$error = 'Usage of unguarded Model attributes is forbidden for security reasons.';
$phpcsFile->addError($error, $varPointer, self::CODE_EMPTY_GUARDED);
return;
}
if ($modelInstance->getGuarded() !== ['*']) {
$error = 'Usage of unguarded Model attributes is forbidden for security reasons.';
$phpcsFile->addError($error, $varPointer, self::CODE_NON_EMPTY_GUARDED);
return;
}
}
/** @inheritDoc */
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
{
// nothing to do here
}
}