Completed
Pull Request — master (#281)
by Juliette
02:26
created

PHPCompatibility_Sniffs_PHP_NewMultiCatchSniff   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 0 27 4
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewMultiCatch.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author	  Juliette Reinders Folmer <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_NewMultiCatch.
14
 *
15
 * Catching multiple exception types in one statement is available since PHP 7.1.
16
 *
17
 * PHP version 7.1
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author	  Juliette Reinders Folmer <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_NewMultiCatchSniff extends PHPCompatibility_Sniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
	/**
26
	 * Returns an array of tokens this test wants to listen for.
27
	 *
28
	 * @return array
29
	 */
30
	public function register()
31
	{
32
		return array(T_CATCH);
33
34
	}//end register()
35
36
	/**
37
	 * Processes this test, when one of its tokens is encountered.
38
	 *
39
	 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
40
	 * @param int				   $stackPtr  The position of the current token
41
	 *										  in the stack passed in $tokens.
42
	 *
43
	 * @return void
44
	 */
45
	public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
46
	{
47
		if ($this->supportsBelow('7.0') === false) {
48
			return;
49
		}
50
51
		$tokens = $phpcsFile->getTokens();
52
		$token  = $tokens[$stackPtr];
53
54
		// Bow out during live coding.
55
		if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
56
			return;
57
		}
58
		
59
		$hasBitwiseOr = $phpcsFile->findNext(T_BITWISE_OR, $token['parenthesis_opener'], $token['parenthesis_closer']);
60
		
61
		if ($hasBitwiseOr === false) {
62
			return;
63
		}
64
		
65
		$phpcsFile->addError(
66
			'Catching multiple exceptions within one statement is not supported in PHP 7.0 or earlier.',
67
			$hasBitwiseOr,
68
			'Found'
69
		);
70
71
	}//end process()
72
73
}//end class
74