Completed
Push — php70 ( 1282e6...33bebb )
by Wim
02:20
created

NewAnonymousClassesSniff   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 17 4
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewAnonymousClasses.
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_NewAnonymousClasses.
14
 *
15
 * Anonymous classes are supported in PHP 7.0
16
 *
17
 * @category  PHP
18
 * @package   PHPCompatibility
19
 * @author    Wim Godden <[email protected]>
20
 */
21
class PHPCompatibility_Sniffs_PHP_NewAnonymousClassesSniff extends PHPCompatibility_Sniff
22
{
23
    /**
24
     * Returns an array of tokens this test wants to listen for.
25
     *
26
     * @return array
27
     */
28
    public function register()
29
    {
30
        return array(T_NEW);
31
    }//end register()
32
33
34
    /**
35
     * Processes this test, when one of its tokens is encountered.
36
     *
37
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
38
     * @param int                  $stackPtr  The position of the current token in the
39
     *                                        stack passed in $tokens.
40
     *
41
     * @return void
42
     */
43
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
44
    {
45
        $tokens = $phpcsFile->getTokens();
0 ignored issues
show
Unused Code introduced by
$tokens is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46
        
47
        $whitespace = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, $stackPtr + 2);
48
        $class = $phpcsFile->findNext(T_ANON_CLASS, $stackPtr + 2, $stackPtr + 3);
49
        if ($whitespace == false || $class == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $whitespace of type integer|false against false; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $class of type integer|false against false; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
50
            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...
51
        }
52
        
53
        if ($this->supportsAbove('7.0')) {
54
            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...
55
        } else {
56
            $phpcsFile->addError('Anonymous classes are not supported in PHP 5.6 or earlier', $stackPtr);
57
        }
58
59
    }//end process()
60
61
62
}//end class
63