Completed
Push — feature/issue-49 ( 9a3dca...dba810 )
by Mikaël
28:22
created

LengthRule::applyRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 1
b 0
f 1
cc 1
eloc 7
nc 1
nop 3
crap 2
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
class LengthRule extends AbstractRule
6
{
7
    /**
8
     * @see \WsdlToPhp\PackageGenerator\File\Validation\AbstractValidation::addRule()
9
     * @param string $parameterName
10
     * @param mixed $value
11
     * @param bool $itemType
12
     * @return Enumeration
13
     */
14
    public function applyRule($parameterName, $value, $itemType = false)
15
    {
16
        $this
17
            ->getMethod()
18
                ->addChild(sprintf('if ((is_scalar(%1$s) && strlen(%1$s) !== %2$d) || (is_array(%1$s) && count(%1$s) !== %2$d)) {', $parameterName, $value))
19
                    ->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(\'Invalid length, please provide an array with %1$d element(s) or a scalar of %1$d character(s)\', __LINE__);', $value), 1))
20
                ->addChild('}');
21
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (WsdlToPhp\PackageGenerat...e\Validation\LengthRule) is incompatible with the return type declared by the abstract method WsdlToPhp\PackageGenerat...AbstractRule::applyRule of type WsdlToPhp\PackageGenerat...tion\AbstractValidation.

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