NumberProcessor::hydrate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Hydrator package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\PhpApi\Hydrator\Processors\Scalar;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ScalarSchema;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
13
use KleijnWeb\PhpApi\Hydrator\Exception\UnsupportedException;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class NumberProcessor extends ScalarProcessor
19
{
20
    /**
21
     * NumberHydrator constructor.
22
     * @param ScalarSchema $schema
23
     */
24
    public function __construct(ScalarSchema $schema)
25
    {
26
        parent::__construct($schema);
27
    }
28
29
    /**
30
     * Cast a scalar value using the schema.
31
     *
32
     * @param mixed  $value
33
     * @param Schema $schema
0 ignored issues
show
Bug introduced by
There is no parameter named $schema. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
34
     *
35
     * @return float|int|string|\DateTimeInterface
36
     * @throws UnsupportedException
37
     */
38
    public function hydrate($value)
39
    {
40
        if ($value === null) {
41
            return $this->schema->getDefault();
42
        }
43
44
        if (!is_string($value)) {
45
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $value; (object|integer|double|null|array|boolean) is incompatible with the return type documented by KleijnWeb\PhpApi\Hydrato...umberProcessor::hydrate of type double|integer|string|DateTimeInterface.

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...
46
        }
47
48
        return !ctype_digit($value) ? (float)$value : (int)$value;
49
    }
50
}
51