MarioBlazek /
Toggable
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Marek\Toggable\Hydrator\Strategy; |
||
| 4 | |||
| 5 | use DateTime; |
||
| 6 | use Marek\Toggable\Hydrator\StrategyInterface; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class DateStrategy |
||
| 10 | * @package Marek\Toggable\Hydrator\Strategy |
||
| 11 | */ |
||
| 12 | class DateStrategy implements StrategyInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * {@inheritdoc} |
||
| 16 | */ |
||
| 17 | 12 | public function hydrate($value) |
|
| 18 | { |
||
| 19 | 12 | if (is_string($value)) { |
|
| 20 | 11 | $value = new DateTime($value); |
|
| 21 | } |
||
| 22 | |||
| 23 | 12 | return $value; |
|
|
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | 12 | public function extract($value) |
|
| 30 | { |
||
| 31 | 12 | if ($value instanceof DateTime) { |
|
| 32 | 10 | $value = $value->format('c'); |
|
| 33 | } |
||
| 34 | |||
| 35 | 12 | return $value; |
|
|
0 ignored issues
–
show
The return type of
return $value; (string|object) is incompatible with the return type declared by the interface Marek\Toggable\Hydrator\StrategyInterface::extract of type array.
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...
|
|||
| 36 | } |
||
| 37 | } |
||
| 38 |