EnvironmentVoter   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 14
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A vote() 0 8 2
1
<?php
2
3
namespace Jlis\Judge\Voters;
4
5
use Illuminate\Support\Facades\App;
6
use Jlis\Judge\Contracts\VoterInterface;
7
8
/**
9
 * @author Julius Ehrlich <[email protected]>
10
 */
11
class EnvironmentVoter implements VoterInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 2
    public function vote($parameter = null, $user = null, array $additional = [])
17
    {
18 2
        if (empty($parameter)) {
19
            return false;
20
        }
21
22 2
        return App::environment($parameter);
0 ignored issues
show
Unused Code introduced by
The call to App::environment() has too many arguments starting with $parameter.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug Best Practice introduced by
The return type of return \Illuminate\Suppo...nvironment($parameter); (string) is incompatible with the return type declared by the interface Jlis\Judge\Contracts\VoterInterface::vote of type boolean.

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...
23
    }
24
}
25