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

FieldRendererFixedText   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 67
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B render() 0 30 2
A factory() 0 6 1
1
<?php
2
3
namespace Graze\CiffRenderer\Renderer\FieldRenderer;
4
5
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererInterface;
6
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
7
use Intervention\Image\ImageManager;
8
use Intervention\Image\Gd\Font;
9
10
class FieldRendererFixedText implements FieldRendererInterface
11
{
12
    const FONT_SIZE_MULTIPLIER = 4.2;
13
14
    const LETTER_SPACING_ADJUSTMENT = 0.97;
15
16
    /**
17
     * @var Font
18
     */
19
    private $font;
20
21
    /**
22
     * @param Font $font
23
     */
24 2
    public function __construct(Font $font)
25
    {
26 2
        $this->font = $font;
27 2
    }
28
29
    /**
30
     * @param ImageManager $imageManager
31
     * @param FieldParserInterface $parser
32
     * @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...
33
     * @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...
34
     * @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...
35
     */
36 1
    public function render(
37
        ImageManager $imageManager,
38
        FieldParserInterface $parser,
39
        callable $fontResolver = null,
40
        callable $graphicResolver = null
41
    ) {
42
        // create the text here so it can be measured
43 1
        $this->font->text($parser->getText());
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...
44 1
        $this->font->file($fontResolver($parser->getFontFace()));
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 getFontFace() 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...
45 1
        $this->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...
46 1
        $this->font->color('#000');
47 1
        $this->font->valign('top');
48
49 1
        $fontCallback = function (&$font) {
50
            // override the font created by Canvas::text()
51 1
            $font = $this->font;
52 1
        };
53
54 1
        $size = $this->font->getBoxSize();
55 1
        $image = $imageManager->canvas($size['width'], $size['height']);
56 1
        $image->text('', null, null, $fontCallback);
57 1
        $image->resize($size['width'] * self::LETTER_SPACING_ADJUSTMENT, $size['height']);
58
59 1
        $oriontation = $parser->getOrientation();
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 getOrientation() 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...
60 1
        if ($oriontation) {
61 1
            $image->rotate(360 - $oriontation);
62
        }
63
64 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...
65
    }
66
67
    /**
68
     * @return FieldParserInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be FieldRendererFixedText?

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...
69
     */
70 1
    public static function factory()
71
    {
72 1
        return new static(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static(new \I...ntion\Image\Gd\Font()); (Graze\CiffRenderer\Rende...\FieldRendererFixedText) 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...
73 1
            new Font()
74
        );
75
    }
76
}
77