These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * HiAPI Yii2 base project for building API |
||
4 | * |
||
5 | * @link https://github.com/hiqdev/hiapi |
||
6 | * @package hiapi |
||
7 | * @license BSD-3-Clause |
||
8 | * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
||
9 | */ |
||
10 | |||
11 | namespace hiapi\commands; |
||
12 | |||
13 | use hiqdev\yii\DataMapper\components\EntityManagerInterface; |
||
14 | use hiqdev\yii\DataMapper\query\Specification; |
||
15 | use hiqdev\yii\DataMapper\repositories\BaseRepository; |
||
16 | |||
17 | /** |
||
18 | * Class GetInfoHandler |
||
19 | * |
||
20 | * @author Andrii Vasyliev <[email protected]> |
||
21 | */ |
||
22 | class GetInfoHandler extends SearchHandler |
||
23 | { |
||
24 | public function handle(EntityCommandInterface $command) |
||
25 | { |
||
26 | return $this->getRepository($command)->findOneOrFail($this->buildSpecification($command)); |
||
0 ignored issues
–
show
The return type of
return $this->getReposit...ecification($command)); (false|object ) is incompatible with the return type of the parent method hiapi\commands\SearchHandler::handle of type object[] .
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
Loading history...
|
|||
27 | } |
||
28 | |||
29 | protected function buildSpecification(EntityCommandInterface $command) |
||
30 | { |
||
31 | return $this->createSpecification()->where(['id' => $command->id])->limit(1); |
||
0 ignored issues
–
show
Accessing
id on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
32 | } |
||
33 | } |
||
34 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.