Completed
Push — php70 ( 992172...1282e6 )
by Wim
04:08
created

ConstantArraysUsingDefineSniff   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 7
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 0 38 6
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_ConstantArraysUsingDefineSniff.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_ConstantArraysUsingDefineSniff.
14
 *
15
 * Constant arrays using define in PHP 7.0
16
 *
17
 * PHP version 7.0
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author    Wim Godden <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_ConstantArraysUsingDefineSniff extends PHPCompatibility_Sniff
24
{
25
26
    /**
27
     * Returns an array of tokens this test wants to listen for.
28
     *
29
     * @return array
30
     */
31
    public function register()
32
    {
33
        return array(T_STRING);
34
    }
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 in the
41
     *                                        stack passed in $tokens.
42
     *
43
     * @return void
44
     */
45
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
46
    {
47
        $tokens = $phpcsFile->getTokens();
48
        
49
        $ignore = array(
50
            T_DOUBLE_COLON,
51
            T_OBJECT_OPERATOR,
52
            T_FUNCTION,
53
            T_CONST,
54
        );
55
        
56
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
57
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
58
            // Not a call to a PHP function.
59
            return;
60
        }
61
        
62
        $function = strtolower($tokens[$stackPtr]['content']);
63
        
64
        if ($function === 'define') {
65
            $openParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, null, null, true);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
            if ($openParenthesis === false) {
67
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface PHP_CodeSniffer_Sniff::process of type null|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
            }
69
            
70
            $comma = $phpcsFile->findNext(T_COMMA, $openParenthesis, $tokens[$openParenthesis]['parenthesis_closer']);
71
            
72
            $array = $phpcsFile->findNext(array(T_ARRAY, T_OPEN_SHORT_ARRAY), $comma, $tokens[$openParenthesis]['parenthesis_closer']);
0 ignored issues
show
Security Bug introduced by
It seems like $comma defined by $phpcsFile->findNext(T_C...['parenthesis_closer']) on line 70 can also be of type false; however, PHP_CodeSniffer_File::findNext() 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...
73
            
74
            if ($array !== false) {
75
                if ($this->supportsAbove('7.0')) {
76
                    return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface PHP_CodeSniffer_Sniff::process of type null|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
77
                } else {
78
                    $phpcsFile->addError('Constant arrays using define are not allowed in PHP 5.6 or earlier', $array);
79
                }
80
            }
81
        }
82
    }
83
}
84