1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Zenify |
5
|
|
|
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ZenifyCodingStandard\Sniffs\Scope; |
9
|
|
|
|
10
|
|
|
use PHP_CodeSniffer_File; |
11
|
|
|
use PHP_CodeSniffer_Standards_AbstractScopeSniff; |
12
|
|
|
use PHP_CodeSniffer_Tokens; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Rules: |
17
|
|
|
* - Function "%s" should have scope modifier. |
18
|
|
|
* - Interface function "%s" should not have scope modifier. |
19
|
|
|
*/ |
20
|
|
|
class MethodScopeSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff |
21
|
|
|
{ |
22
|
|
|
|
23
|
1 |
|
public function __construct() |
24
|
|
|
{ |
25
|
1 |
|
parent::__construct([T_CLASS, T_INTERFACE], [T_FUNCTION]); |
26
|
1 |
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
1 |
|
protected function processTokenWithinScope(PHP_CodeSniffer_File $file, $position, $currScope) |
33
|
|
|
{ |
34
|
1 |
|
$tokens = $file->getTokens(); |
35
|
|
|
|
36
|
1 |
|
$isClass = $tokens[$currScope]['code'] === T_CLASS; |
37
|
|
|
|
38
|
1 |
|
$methodName = $file->getDeclarationName($position); |
39
|
1 |
|
if ($methodName === NULL) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
$pCurly = $file->findPrevious(T_CLOSE_CURLY_BRACKET, $position); |
44
|
1 |
|
$modifier = $file->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $position, max($currScope, $pCurly)); |
|
|
|
|
45
|
|
|
|
46
|
1 |
|
if ($isClass) { |
47
|
1 |
|
if (($modifier === FALSE) || ($tokens[$modifier]['line'] !== $tokens[$position]['line'])) { |
48
|
1 |
|
$error = 'Function "%s" should have scope modifier.'; |
49
|
1 |
|
$data = [$methodName]; |
50
|
1 |
|
$file->addError($error, $position, '', $data); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} else { |
54
|
1 |
|
if ($modifier !== FALSE) { |
55
|
1 |
|
$error = 'Interface function "%s" should not have scope modifier.'; |
56
|
1 |
|
$data = [$methodName]; |
57
|
1 |
|
$file->addError($error, $position, '', $data); |
58
|
|
|
} |
59
|
|
|
} |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.