Completed
Push — refactor-04-parser-tests ( bd4663...403d11 )
by John
02:57
created

FieldRendererFactory::getFieldRenderer()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
crap 4
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...
46
47 1
            default:
48 1
                $renderer = FieldRendererFixedText::class;
49 1
                break;
50 3
        }
51
52 3
        return $renderer::factory();
53
    }
54
55
    /**
56
     * @return GraphicPrimitiveFactory
0 ignored issues
show
Documentation introduced by
Should the return type not be FieldRendererFactory?

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...
57
     */
58
    public static function factory()
59
    {
60
        return new static(
61
            new GraphicPrimitiveFactory()
62
        );
63
    }
64
}
65