GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 699b70...879176 )
by Chris
13:23
created

DocBlockAlignmentSniff::process()   F

Complexity

Conditions 25
Paths 1110

Size

Total Lines 96
Code Lines 64

Duplication

Lines 6
Ratio 6.25 %

Importance

Changes 0
Metric Value
cc 25
eloc 64
nc 1110
nop 2
dl 6
loc 96
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://pear.php.net/package/PHP_CodeSniffer_CakePHP
12
 * @since         CakePHP CodeSniffer 1.0.0
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
16
namespace PSR2R\Sniffs\WhiteSpace;
17
18
use PHP_CodeSniffer_File;
19
use PSR2R\Tools\AbstractSniff;
20
21
/**
22
 * Ensures doc block alignment with its code.
23
 *
24
 * @author Mark Scherer
25
 * @license MIT
26
 */
27
class DocBlockAlignmentSniff extends AbstractSniff {
28
29
	/**
30
	 * @inheritDoc
31
	 */
32
	public function register() {
33
		return [T_DOC_COMMENT_OPEN_TAG];
34
	}
35
36
	/**
37
	 * @inheritDoc
38
	 */
39
	public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
40
		$tokens = $phpcsFile->getTokens();
41
		$leftWall = [
42
			T_CLASS,
43
			T_NAMESPACE,
44
			T_INTERFACE,
45
			T_TRAIT,
46
			T_USE
47
		];
48
		$oneIndentation = [
49
			T_FUNCTION,
50
			T_VARIABLE,
51
			T_CONST
52
		];
53
		$allTokens = array_merge($leftWall, $oneIndentation);
54
		$isNotFlatFile = $phpcsFile->findNext(T_NAMESPACE, 0);
55
		$nextIndex = $phpcsFile->findNext($allTokens, $stackPtr + 1);
56
57
		$expectedColumn = $tokens[$nextIndex]['column'];
58
		$firstNonWhitespaceIndex = $this->findFirstNonWhitespaceInLine($phpcsFile, $nextIndex);
0 ignored issues
show
Security Bug introduced by
It seems like $nextIndex defined by $phpcsFile->findNext($allTokens, $stackPtr + 1) on line 55 can also be of type false; however, PSR2R\Sniffs\WhiteSpace\...stNonWhitespaceInLine() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
59
		if ($firstNonWhitespaceIndex) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $firstNonWhitespaceIndex of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
60
			$expectedColumn = $tokens[$firstNonWhitespaceIndex]['column'];
61
		}
62
63
		// We should allow for tabs and spaces
64
		$expectedColumnAdjusted = $expectedColumn;
65
		if ($expectedColumn === 2) {
66
			$expectedColumnAdjusted = 5;
67
		} elseif ($expectedColumn === 5) {
68
			$expectedColumnAdjusted = 2;
69
		}
70
71
		if ($nextIndex) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $nextIndex of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
72
			$isNotWalled = (in_array($tokens[$nextIndex]['code'], $leftWall) && $tokens[$stackPtr]['column'] !== 1);
73
			$isNotIndented = false;
74
			if ($isNotFlatFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $isNotFlatFile of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
75
				$isNotIndented = (in_array($tokens[$nextIndex]['code'], $oneIndentation) && $tokens[$stackPtr]['column'] !== $expectedColumn && $tokens[$stackPtr]['column'] !== $expectedColumnAdjusted);
76
			}
77
			if ($isNotWalled || $isNotIndented) {
78
				$fix = $phpcsFile->addFixableError('Expected docblock to be aligned with code.', $stackPtr, 'NotAllowed');
79
				if ($fix) {
80
					$docBlockEndIndex = $tokens[$stackPtr]['comment_closer'];
81
82
					if ($isNotWalled) {
83
						$prevIndex = $stackPtr - 1;
84
						if ($tokens[$prevIndex]['code'] !== T_WHITESPACE) {
85
							return;
86
						}
87
88
						$phpcsFile->fixer->beginChangeset();
89
90
						$this->outdent($phpcsFile, $prevIndex);
91
92
						for ($i = $stackPtr; $i <= $docBlockEndIndex; $i++) {
93 View Code Duplication
							if (!$this->isGivenKind(T_DOC_COMMENT_WHITESPACE, $tokens[$i]) || $tokens[$i]['column'] !== 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
								continue;
95
							}
96
							$this->outdent($phpcsFile, $i);
97
						}
98
						$phpcsFile->fixer->endChangeset();
99
						return;
100
					}
101
102
					if ($isNotIndented) {
103
						// + means too much indentation (we need to outdend), - means not enough indentation (needs indenting)
104
						if ($tokens[$stackPtr]['column'] < $expectedColumnAdjusted) {
105
							$diff = $tokens[$stackPtr]['column'] - $expectedColumn;
106
						} else {
107
							$diff = ($tokens[$stackPtr]['column'] - $expectedColumnAdjusted) / 4;
108
						}
109
110
						$phpcsFile->fixer->beginChangeset();
111
112
						$prevIndex = $stackPtr - 1;
113
						if ($diff < 0 && $tokens[$prevIndex]['line'] !== $tokens[$stackPtr]['line']) {
114
							$phpcsFile->fixer->addContentBefore($stackPtr, str_repeat("\t", -$diff));
115
						} else {
116
							$this->outdent($phpcsFile, $prevIndex);
117
						}
118
119
						for ($i = $stackPtr; $i <= $docBlockEndIndex; $i++) {
120 View Code Duplication
							if (!$this->isGivenKind(T_DOC_COMMENT_WHITESPACE, $tokens[$i]) || $tokens[$i]['column'] !== 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
								continue;
122
							}
123
							if ($diff < 0) {
124
								$this->indent($phpcsFile, $i, -$diff);
125
							} else {
126
								$this->outdent($phpcsFile, $i, $diff);
127
							}
128
						}
129
						$phpcsFile->fixer->endChangeset();
130
					}
131
				}
132
			}
133
		}
134
	}
135
136
	/**
137
	 * @param \PHP_CodeSniffer_File $phpcsFile
138
	 * @param int $index
139
	 * @return int|null
140
	 */
141
	protected function findFirstNonWhitespaceInLine(PHP_CodeSniffer_File $phpcsFile, $index) {
142
		$tokens = $phpcsFile->getTokens();
143
144
		$firstIndex = $index;
145
		while (!empty($tokens[$firstIndex - 1]) && $tokens[$firstIndex - 1]['line'] === $tokens[$index]['line']) {
146
			$firstIndex--;
147
		}
148
149
		return $phpcsFile->findNext(T_WHITESPACE, $firstIndex, $index, true);
150
	}
151
152
}
153