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

FieldRendererBarcode   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 41
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 21 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
use Graze\CiffRenderer\Renderer\Font\FontFace;
9
10
class FieldRendererBarcode implements FieldRendererInterface
11
{
12
    const FONT_SIZE_MULTIPLIER = 7;
13
14
    /**
15
     * @param ImageManager $imageManager
16
     * @param FieldParserInterface $parser
17
     * @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...
18
     * @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...
19
     * @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...
20
     */
21 1
    public function render(
22
        ImageManager $imageManager,
23
        FieldParserInterface $parser,
24
        callable $fontResolver = null,
25
        callable $graphicResolver = null
26
    ) {
27 1
        $image = $imageManager->canvas($parser->getWidth(), $parser->getHeight(), '#000');
28 1
        $fontPath = $fontResolver(FontFace::FACE_BARCODE);
29
30 1
        $fontCallback = function ($font) use ($fontPath, $parser) {
31 1
            $font->file($fontPath);
32 1
            $font->size($parser->getFontSize() * self::FONT_SIZE_MULTIPLIER);
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 getFontSize() does only exist in the following implementations of said interface: Graze\CiffRenderer\Parse...AbstractFieldParserDate, Graze\CiffRenderer\Parse...rser\FieldParserBarcode, Graze\CiffRenderer\Parse...dParser\FieldParserDate, Graze\CiffRenderer\Parse...r\FieldParserDateOffset, Graze\CiffRenderer\Parse...er\FieldParserFixedText.

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...
33 1
            $font->color('#fff');
34 1
            $font->align('center');
35 1
            $font->valign('middle');
36 1
        };
37
38 1
        $image->text($parser->getText(), $parser->getWidth() / 2, $parser->getHeight(), $fontCallback);
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 getText() does only exist in the following implementations of said interface: Graze\CiffRenderer\Parse...AbstractFieldParserDate, Graze\CiffRenderer\Parse...rser\FieldParserBarcode, Graze\CiffRenderer\Parse...dParser\FieldParserDate, Graze\CiffRenderer\Parse...r\FieldParserDateOffset, Graze\CiffRenderer\Parse...er\FieldParserFixedText.

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...
39
40 1
        return $image;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $image; (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...
41
    }
42
43
    /**
44
     * @return FieldParserInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be FieldRendererBarcode?

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...
45
     */
46 1
    public static function factory()
47
    {
48 1
        return new static();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static(); (Graze\CiffRenderer\Rende...er\FieldRendererBarcode) 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...
49
    }
50
}
51