Completed
Push — refactor-04-parser-tests ( dc4950...bd4663 )
by John
06:06
created

FieldRendererFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 51
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFieldRenderer() 0 21 4
A factory() 0 6 1
1
<?php
2
3
namespace Graze\CiffRenderer\Renderer\FieldRenderer;
4
5
use Graze\CiffRenderer\Renderer\FieldRenderer\GraphicPrimitive\GraphicPrimitiveFactory;
6
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
7
use Graze\CiffRenderer\Parser\FieldParser\FieldParserBarcode;
8
use Graze\CiffRenderer\Parser\FieldParser\FieldParserStaticGraphic;
9
use Graze\CiffRenderer\Parser\FieldParser\FieldParserGraphicPrimitive;
10
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererBarcode;
11
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererFixedText;
12
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererStaticGraphic;
13
14
class FieldRendererFactory
15
{
16
    /**
17
     * @var GraphicPrimitiveFactory
18
     */
19
    private $graphicPrimitiveFactory;
20
21
    /**
22
     * @param GraphicPrimitiveFactory $graphicPrimitiveFactory
23
     */
24 4
    public function __construct(GraphicPrimitiveFactory $graphicPrimitiveFactory)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $graphicPrimitiveFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
25
    {
26 4
        $this->graphicPrimitiveFactory = $graphicPrimitiveFactory;
27 4
    }
28
29
    /**
30
     * @param FieldParserInterface $parser
31
     * @return Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererInterface
32
     */
33 4
    public function getFieldRenderer(FieldParserInterface $parser)
34
    {
35 4
        switch (get_class($parser)) {
36 4
            case FieldParserBarcode::class:
37 1
                $renderer = FieldRendererBarcode::class;
38 1
                break;
39
40 3
            case FieldParserStaticGraphic::class:
41 1
                $renderer = FieldRendererStaticGraphic::class;
42 1
                break;
43
44 2
            case FieldParserGraphicPrimitive::class:
45 1
                return $this->graphicPrimitiveFactory->getFieldRenderer($parser->getShape());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\CiffRenderer\Parse...er\FieldParserInterface as the method getShape() does only exist in the following implementations of said interface: Graze\CiffRenderer\Parse...dParserGraphicPrimitive.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
Bug Best Practice introduced by
The return type of return $this->graphicPri...r($parser->getShape()); (Graze\CiffRenderer\Rende...\FieldRendererInterface) is incompatible with the return type documented by Graze\CiffRenderer\Rende...ctory::getFieldRenderer of type Graze\CiffRenderer\Rende...\FieldRendererInterface.

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
            default:
48 1
                $renderer = FieldRendererFixedText::class;
49 1
                break;
50
        }
51
52 3
        return $renderer::factory();
53
    }
54
55
    /**
56
     * @return FieldRendererFactory
57
     */
58
    public static function factory()
59
    {
60
        return new static(
61
            new GraphicPrimitiveFactory()
62
        );
63
    }
64
}
65