Completed
Pull Request — master (#41)
by Tomáš
10:18 queued 07:10
created

YodaConditionSniff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 78
ccs 26
cts 27
cp 0.963
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 13 1
A process() 0 13 3
A getPreviousNonEmptyToken() 0 9 2
A isExpressionToken() 0 7 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of Zenify
7
 * Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
8
 */
9
10
namespace ZenifyCodingStandard\Sniffs\ControlStructures;
11
12
use PHP_CodeSniffer_File;
13
use PHP_CodeSniffer_Sniff;
14
15
16
/**
17
 * Rules:
18
 * - Yoda condition should not be used; switch expression order
19
 */
20
final class YodaConditionSniff implements PHP_CodeSniffer_Sniff
21
{
22
23
	/**
24
	 * @var string
25
	 */
26
	const MESSAGE_ERROR = 'Yoda condition should not be used; switch expression order';
27
28
	/**
29
	 * @var int
30
	 */
31
	private $position;
32
33
	/**
34
	 * @var PHP_CodeSniffer_File
35
	 */
36
	private $file;
37
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42 1
	public function register()
43
	{
44
		return [
45 1
			T_IS_IDENTICAL,
46 1
			T_IS_NOT_IDENTICAL,
47 1
			T_IS_EQUAL,
48 1
			T_IS_NOT_EQUAL,
49 1
			T_GREATER_THAN,
50 1
			T_LESS_THAN,
51 1
			T_IS_GREATER_OR_EQUAL,
52 1
			T_IS_SMALLER_OR_EQUAL
53
		];
54
	}
55
56
57
	/**
58
	 * {@inheritdoc}
59
	 */
60 1
	public function process(PHP_CodeSniffer_File $file, $position)
61
	{
62 1
		$this->file = $file;
63 1
		$this->position = $position;
64
65 1
		$previousNonEmptyToken = $this->getPreviousNonEmptyToken();
66
67 1
		if ($previousNonEmptyToken) {
68 1
			if ($this->isExpressionToken($previousNonEmptyToken)) {
0 ignored issues
show
Bug introduced by
It seems like $previousNonEmptyToken defined by $this->getPreviousNonEmptyToken() on line 65 can also be of type boolean; however, ZenifyCodingStandard\Sni...ff::isExpressionToken() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69 1
				$file->addError(self::MESSAGE_ERROR, $position);
70
			}
71
		}
72 1
	}
73
74
75
	/**
76
	 * @return array|bool
77
	 */
78 1
	private function getPreviousNonEmptyToken()
79
	{
80 1
		$leftTokenPosition = $this->file->findPrevious(T_WHITESPACE, ($this->position - 1), NULL, TRUE);
81 1
		$tokens = $this->file->getTokens();
82 1
		if ($leftTokenPosition) {
83 1
			return $tokens[$leftTokenPosition];
84
		}
85
		return FALSE;
86
	}
87
88
89 1
	private function isExpressionToken(array $token) : bool
90
	{
91 1
		if (in_array($token['code'], [T_MINUS, T_NULL, T_FALSE, T_TRUE, T_LNUMBER, T_CONSTANT_ENCAPSED_STRING])) {
92 1
			return TRUE;
93
		}
94 1
		return FALSE;
95
	}
96
97
}
98