Completed
Push — refactor-01-decoupling ( 56cf28...3120c6 )
by John
03:43
created

FieldRendererStaticGraphic   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 27
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 9 1
A factory() 0 4 1
1
<?php
2
3
namespace Graze\CiffRenderer\Renderer\FieldRenderer;
4
5
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererInterface;
6
use Intervention\Image\ImageManager;
7
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
8
9
class FieldRendererStaticGraphic implements FieldRendererInterface
10
{
11
    /**
12
     * @param ImageManager $imageManager
13
     * @param FieldParserInterface $parser
14
     * @param callable $fontResolver
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fontResolver not be null|callable?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
15
     * @param callable $graphicResolver
0 ignored issues
show
Documentation introduced by
Should the type for parameter $graphicResolver not be null|callable?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
16
     * @return Intervention\Image\Image
0 ignored issues
show
Documentation introduced by
Should the return type not be \Intervention\Image\Image?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
17
     */
18 1
    public function render(
19
        ImageManager $imageManager,
20
        FieldParserInterface $parser,
21
        callable $fontResolver = null,
22
        callable $graphicResolver = null
23
    ) {
24 1
        $gdResource = $graphicResolver($parser->getGraphic());
25 1
        return $imageManager->make($gdResource);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $imageManager->make($gdResource); (Intervention\Image\Image) is incompatible with the return type declared by the interface Graze\CiffRenderer\Rende...ndererInterface::render of type Graze\CiffRenderer\Rende...ntervention\Image\Image.

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...
26
    }
27
28
    /**
29
     * @return FieldRendererInterface
30
     */
31 1
    public static function factory()
32
    {
33 1
        return new static();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static(); (Graze\CiffRenderer\Rende...ldRendererStaticGraphic) is incompatible with the return type declared by the interface Graze\CiffRenderer\Rende...dererInterface::factory of type Graze\CiffRenderer\Parse...er\FieldParserInterface.

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...
34
    }
35
}
36