Completed
Push — master ( b79486...df4d3f )
by Alexander
02:06
created

ClassCreateInstanceSniff::process()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 36
nc 10
nop 2
dl 0
loc 51
ccs 41
cts 41
cp 1
crap 7
rs 6.9743
c 0
b 0
f 0

How to fix   Long Method   

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
 * CodingStandard_Sniffs_Classes_ClassCreateInstanceSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Peter Philipp <[email protected]>
10
 * @author   Alexander Obuhovich <[email protected]>
11
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
12
 * @link     https://github.com/aik099/CodingStandard
13
 */
14
15
/**
16
 * Class create instance Test.
17
 *
18
 * Checks the declaration of the class is correct.
19
 *
20
 * @category PHP
21
 * @package  PHP_CodeSniffer
22
 * @author   Peter Philipp <[email protected]>
23
 * @author   Alexander Obuhovich <[email protected]>
24
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
25
 * @link     https://github.com/aik099/CodingStandard
26
 */
27
class CodingStandard_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_Sniff
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
28
{
29
30
31
    /**
32
     * Returns an array of tokens this test wants to listen for.
33
     *
34
     * @return integer[]
35
     */
36 1
    public function register()
37
    {
38 1
        return array(T_NEW);
39
40
    }//end register()
41
42
43
    /**
44
     * Processes this test, when one of its tokens is encountered.
45
     *
46
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
47
     * @param int                  $stackPtr  The position of the current token in the
48
     *                                        stack passed in $tokens.
49
     *
50
     * @return void
51
     */
52 1
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
53
    {
54 1
        $scopeEnd = null;
55 1
        $tokens   = $phpcsFile->getTokens();
56
57 1
        if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
58
            // New in PHP 5.4: allow to instantiate class and immediately call method on it.
59 1
            list (, $scopeEnd) = each($tokens[$stackPtr]['nested_parenthesis']);
60 1
        }
61
62 1
        $nextParenthesis = $phpcsFile->findNext(
63 1
            T_OPEN_PARENTHESIS,
64 1
            ($stackPtr + 1),
65 1
            $scopeEnd,
66 1
            false,
67 1
            null,
68
            true
69 1
        );
70
71 1
        if ($nextParenthesis === false || $tokens[$nextParenthesis]['line'] !== $tokens[$stackPtr]['line']) {
72 1
            $error = 'Calling class constructors must always include parentheses';
73 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr);
74 1
            if ($fix === true) {
75 1
                $phpcsFile->fixer->beginChangeset();
76 1
                $classNameEnd = $phpcsFile->findNext(
77
                    array(
78 1
                     T_WHITESPACE,
79 1
                     T_NS_SEPARATOR,
80 1
                     T_STRING,
81 1
                    ),
82 1
                    ($stackPtr + 1),
83 1
                    null,
84 1
                    true,
85 1
                    null,
86
                    true
87 1
                );
88
89 1
                $phpcsFile->fixer->addContentBefore($classNameEnd, '()');
0 ignored issues
show
Security Bug introduced by
It seems like $classNameEnd defined by $phpcsFile->findNext(arr...null, true, null, true) on line 76 can also be of type false; however, PHP_CodeSniffer_Fixer::addContentBefore() 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...
90 1
                $phpcsFile->fixer->endChangeset();
91 1
            }//end if
92 1
        } else if ($tokens[($nextParenthesis - 1)]['code'] === T_WHITESPACE) {
93 1
            $error = 'Between the class name and the opening parenthesis spaces are not welcome';
94 1
            $fix   = $phpcsFile->addFixableError($error, ($nextParenthesis - 1));
95 1
            if ($fix === true) {
96 1
                $phpcsFile->fixer->beginChangeset();
97 1
                $phpcsFile->fixer->replaceToken(($nextParenthesis - 1), '');
98 1
                $phpcsFile->fixer->endChangeset();
99 1
            }//end if
100 1
        }//end if
101
102 1
    }//end process()
103
104
105
}//end class
106