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

UseDeclarationSniff::process()   F

Complexity

Conditions 23
Paths 5833

Size

Total Lines 100
Code Lines 62

Duplication

Lines 46
Ratio 46 %

Importance

Changes 0
Metric Value
cc 23
eloc 62
nc 5833
nop 2
dl 46
loc 100
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
 * PSR2_Sniffs_Namespaces_UseDeclarationSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <[email protected]>
10
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
13
 */
14
15
namespace PSR2R\Sniffs\Namespaces;
16
17
use PHP_CodeSniffer_File;
18
use PHP_CodeSniffer_Sniff;
19
use PHP_CodeSniffer_Tokens;
20
use PSR2R\Tools\Traits\CommentingTrait;
21
use PSR2R\Tools\Traits\NamespaceTrait;
22
23
/**
24
 * PSR2_Sniffs_Namespaces_UseDeclarationSniff.
25
 *
26
 * Ensures USE blocks are declared correctly.
27
 *
28
 * @author Greg Sherwood <[email protected]>
29
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
30
 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31
 * @version Release: @package_version@
32
 * @link http://pear.php.net/package/PHP_CodeSniffer
33
 */
34
class UseDeclarationSniff implements PHP_CodeSniffer_Sniff {
35
36
	use CommentingTrait;
37
	use NamespaceTrait;
38
39
	/**
40
	 * @inheritDoc
41
	 */
42
	public function register() {
43
		return [T_USE];
44
	}
45
46
	/**
47
	 * @inheritDoc
48
	 */
49
	public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
50
		if ($this->shouldIgnoreUse($phpcsFile, $stackPtr) === true) {
51
			return;
52
		}
53
54
		$tokens = $phpcsFile->getTokens();
55
56
		// One space after the use keyword.
57 View Code Duplication
		if ($tokens[($stackPtr + 1)]['content'] !== ' ') {
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...
58
			$error = 'There must be a single space after the USE keyword';
59
			$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse');
60
			if ($fix === true) {
61
				$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
62
			}
63
		}
64
65
		// Namespaces in use statements must not have a leading separator
66
		$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
67
		if ($tokens[$next]['code'] === T_NS_SEPARATOR) {
68
			$error = 'Namespaces in use statements should not start with a namespace separator';
69
			$fix = $phpcsFile->addFixableError($error, $next, 'NamespaceStart');
0 ignored issues
show
Security Bug introduced by
It seems like $next defined by $phpcsFile->findNext(\PH...ackPtr + 1, null, true) on line 66 can also be of type false; however, PHP_CodeSniffer_File::addFixableError() 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...
70
			if ($fix) {
71
				$phpcsFile->fixer->replaceToken($next, '');
0 ignored issues
show
Security Bug introduced by
It seems like $next defined by $phpcsFile->findNext(\PH...ackPtr + 1, null, true) on line 66 can also be of type false; however, PHP_CodeSniffer_Fixer::replaceToken() 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...
72
			}
73
		}
74
75
		// Only one USE declaration allowed per statement.
76
		$next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON], ($stackPtr + 1));
77
		if ($tokens[$next]['code'] === T_COMMA) {
78
			$error = 'There must be one USE keyword per declaration';
79
			$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations');
80
			if ($fix === true) {
81
				$phpcsFile->fixer->replaceToken($next, ';' . $phpcsFile->eolChar . 'use ');
0 ignored issues
show
Security Bug introduced by
It seems like $next defined by $phpcsFile->findNext(arr...ICOLON), $stackPtr + 1) on line 76 can also be of type false; however, PHP_CodeSniffer_Fixer::replaceToken() 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...
82
			}
83
		} else {
84
			$nextUse = $phpcsFile->findNext(T_USE, $next + 1);
85
			if ($nextUse && !$this->shouldIgnoreUse($phpcsFile, $nextUse)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $nextUse 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...
86
				if ($tokens[$nextUse]['line'] > $tokens[$next]['line'] + 1) {
87
					$error = 'There should not be newlines between use statements';
88
					$fix = $phpcsFile->addFixableError($error, $nextUse, 'NewlineBetweenUse');
89
					if ($fix) {
90
						$phpcsFile->fixer->replaceToken($nextUse - 1, '');
91
					}
92
				}
93
			}
94
		}
95
96
		// Make sure this USE comes after the first namespace declaration.
97
		$prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
98 View Code Duplication
		if ($prev !== false) {
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...
99
			$first = $phpcsFile->findNext(T_NAMESPACE, 1);
100
			if ($prev !== $first) {
101
				$error = 'USE declarations must go after the first namespace declaration';
102
				$phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace');
103
			}
104
		}
105
106
		// Only interested in the last USE statement from here onwards.
107
		$nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1));
108 View Code Duplication
		while ($this->shouldIgnoreUse($phpcsFile, $nextUse)) {
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...
109
			$nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1));
110
			if ($nextUse === false) {
111
				break;
112
			}
113
		}
114
115
		if ($nextUse !== false) {
116
			return;
117
		}
118
119
		$end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
120
		$next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true);
121
		$diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1);
122 View Code Duplication
		if ($diff !== 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...
123
			if ($diff < 0) {
124
				$diff = 0;
125
			}
126
127
			$error = 'There must be one blank line after the last USE statement; %s found;';
128
			$data = [$diff];
129
			$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterLastUse', $data);
130
			if ($fix === true) {
131
				if ($diff === 0) {
132
					$phpcsFile->fixer->addNewline($end);
133
				} else {
134
					$phpcsFile->fixer->beginChangeset();
135
					for ($i = ($end + 1); $i < $next; $i++) {
136
						if ($tokens[$i]['line'] === $tokens[$next]['line']) {
137
							break;
138
						}
139
140
						$phpcsFile->fixer->replaceToken($i, '');
141
					}
142
143
					$phpcsFile->fixer->addNewline($end);
144
					$phpcsFile->fixer->endChangeset();
145
				}
146
			}
147
		}
148
	}
149
150
}
151