Completed
Pull Request — master (#29)
by John
08:43
created

FixedTextRenderer::getFontResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Graze\CiffRenderer\Field\Renderer;
4
5
use Graze\CiffRenderer\Field\Renderer\AbstractdRenderer;
6
use Intervention\Image\Gd\Font;
7
8
class FixedTextRenderer extends AbstractRenderer
9
{
10
    /**
11
     * Increase Ciff's 'pitch' value to approximate size in pixels.
12
     *
13
     * @var float
14
     */
15
    const FONT_SIZE_MULTIPLIER = 4.2;
16
17
    /**
18
     * @var float
19
     */
20
    const LETTER_SPACING_ADJUSTMENT = 0.97;
21
22
    /**
23
     * @var int
24
     */
25
    const DEFAULT_FONT_SIZE = 13;
26
27
    /**
28
     * @var callable
29
     */
30
    protected $fontResolver;
31
32
    /**
33
     * @param callable $fontResolver
34
     */
35
    public function setFontResolver(callable $fontResolver)
36
    {
37
        $this->fontResolver = $fontResolver;
38
    }
39
40
    /**
41
     * @return callable
42
     */
43
    protected function getFontResolver()
44
    {
45
        return $this->fontResolver;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    protected function getFontFace()
52
    {
53
        return $this->parser->getFontFace();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\CiffRenderer\Field\Parser\ParserInterface as the method getFontFace() does only exist in the following implementations of said interface: Graze\CiffRenderer\Field\Parser\BarcodeParser, Graze\CiffRenderer\Field...rser\AbstractDateParser, Graze\CiffRenderer\Field...r\DateParser\DateParser, Graze\CiffRenderer\Field...Parser\OffsetDateParser, Graze\CiffRenderer\Field\Parser\FixedTextParser.

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...
54
    }
55
56
    /**
57
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be double?

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...
58
     */
59
    protected function getFontSize()
60
    {
61
        $size = $this->parser->getFontSize();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\CiffRenderer\Field\Parser\ParserInterface as the method getFontSize() does only exist in the following implementations of said interface: Graze\CiffRenderer\Field\Parser\BarcodeParser, Graze\CiffRenderer\Field...rser\AbstractDateParser, Graze\CiffRenderer\Field...r\DateParser\DateParser, Graze\CiffRenderer\Field...Parser\OffsetDateParser, Graze\CiffRenderer\Field\Parser\FixedTextParser.

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...
62
        if (!$size) {
63
            $size = self::DEFAULT_FONT_SIZE;
64
        }
65
66
        return $size * self::FONT_SIZE_MULTIPLIER;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    protected function getText()
73
    {
74
        return $this->parser->getText();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\CiffRenderer\Field\Parser\ParserInterface as the method getText() does only exist in the following implementations of said interface: Graze\CiffRenderer\Field\Parser\BarcodeParser, Graze\CiffRenderer\Field...rser\AbstractDateParser, Graze\CiffRenderer\Field...r\DateParser\DateParser, Graze\CiffRenderer\Field...Parser\OffsetDateParser, Graze\CiffRenderer\Field\Parser\FixedTextParser.

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...
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    protected function getOrientation()
81
    {
82
        return $this->parser->getOrientation();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\CiffRenderer\Field\Parser\ParserInterface as the method getOrientation() does only exist in the following implementations of said interface: Graze\CiffRenderer\Field\Parser\BarcodeParser, Graze\CiffRenderer\Field...rser\AbstractDateParser, Graze\CiffRenderer\Field...r\DateParser\DateParser, Graze\CiffRenderer\Field...Parser\OffsetDateParser, Graze\CiffRenderer\Field\Parser\FixedTextParser.

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...
83
    }
84
85
    /**
86
     * @return Intervention/Image/Image
0 ignored issues
show
Documentation introduced by
The doc-type Intervention/Image/Image could not be parsed: Unknown type name "Intervention/Image/Image" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
87
     */
88 1
    public function render()
89
    {
90 1
        $fontResolver = $this->getFontResolver();
91
92 1
        $fontPath = $fontResolver($this->getFontFace());
93
94
        // create the text here so it can be measured
95 1
        $fontOverride = new Font($this->getText());
0 ignored issues
show
Documentation introduced by
$this->getText() is of type string, but the function expects a object<Intervention\Image\Strinf>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
97 1
        $fontOverride->file($fontPath);
98 1
        $fontOverride->size($this->getFontSize());
99 1
        $fontOverride->color('#000');
100 1
        $fontOverride->valign('top');
101
102 1
        $size = $fontOverride->getBoxSize();
103
104
        // if text consists of invisible chars, measured size will be zero, nothing to print
105 1
        if ($size['width'] < 1 || $size['height'] < 1) {
106
            return null;
107
        }
108
109 1
        $fontCallback = function (&$font) use ($fontOverride) {
110
            // override the font created by Canvas::text()
111 1
            $font = $fontOverride;
112 1
        };
113
114 1
        $canvas = $this->getImageManager()->canvas($size['width'], $size['height'], $this->getTracerColour());
115
116 1
        $canvas->text('', null, null, $fontCallback);
117
118 1
        $canvas->resize(round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']);
119
120 1
        if ($this->getOrientation()) {
121 1
            $canvas->rotate(360 - $this->getOrientation());
122 1
        }
123
124 1
        return $canvas;
125
    }
126
}
127