Completed
Push — master ( 6c3b78...8b46fe )
by John
04:40
created

FixedTextBuilder::buildRenderer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2.0116
1
<?php
2
3
namespace Graze\CiffRenderer\Field\Builder;
4
5
use Graze\CiffRenderer\Field\Builder\AbstractBuilder;
6
use Graze\CiffRenderer\Field\Parser\FixedTextParser;
7
use Graze\CiffRenderer\Field\Renderer\FixedTextRenderer;
8
use Graze\CiffRenderer\Field\Parser\ParserInterface;
9
use Graze\CiffRenderer\Field\Renderer\RendererInterface;
10
use Graze\CiffRenderer\Exception\RuntimeException;
11
12 View Code Duplication
class FixedTextBuilder extends AbstractBuilder
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /**
15
     * @return FixedTextParser
16
     */
17
    protected function instantiateParser()
18
    {
19
        return new FixedTextParser();
20
    }
21
22
    /**
23
     * @param ParserInterface $parser
24
     *
25
     * @return ParserInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be FixedTextRenderer?

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...
26
     */
27
    protected function instantiateRenderer(ParserInterface $parser)
28
    {
29
        return new FixedTextRenderer();
30
    }
31
32
    /**
33
     * @param RendererInterface $renderer
34
     * @param ParserInterface $parser
35
     *
36
     * @return RendererInterface
37
     *
38
     * @throws RuntimeException
39
     */
40 4
    public function buildRenderer(RendererInterface $renderer, ParserInterface $parser)
41
    {
42 4
        $render = parent::buildRenderer($renderer, $parser);
0 ignored issues
show
Unused Code introduced by
$render is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
44 4
        $fontResolver = $this->getFontResolver();
45 4
        if (!$fontResolver) {
46
            throw new RuntimeException('Font resolver not set');
47
        }
48
49 4
        $renderer->setFontResolver($fontResolver);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\CiffRenderer\Field...derer\RendererInterface as the method setFontResolver() does only exist in the following implementations of said interface: Graze\CiffRenderer\Field\Renderer\BarcodeRenderer, Graze\CiffRenderer\Field...derer\FixedTextRenderer.

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...
50 4
        return $renderer;
51
    }
52
}
53