Completed
Push — refresh ( 3dadd3 )
by Tomáš
03:56
created

MethodScopeSniff::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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));
0 ignored issues
show
Bug introduced by
It seems like max($currScope, $pCurly) targeting max() can also be of type boolean; however, PHP_CodeSniffer_File::findPrevious() does only seem to accept integer|null, maybe add an additional type check?

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.

Loading history...
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