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

MethodScopeSniff   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 43
ccs 20
cts 21
cp 0.9524
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B processTokenWithinScope() 0 29 6
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