Completed
Push — master ( 4984e7...9ad690 )
by John
03:58
created

FixedTextRenderer::getOrientation()   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
    const FONT_SIZE_MULTIPLIER = 4.2;
11
12
    const LETTER_SPACING_ADJUSTMENT = 0.97;
13
14
    /**
15
     * @var callable
16
     */
17
    protected $fontResolver;
18
19
    /**
20
     * @param callable $fontResolver
21
     */
22
    public function setFontResolver(callable $fontResolver)
23
    {
24
        $this->fontResolver = $fontResolver;
25
    }
26
27
    /**
28
     * @return callable
29
     */
30
    protected function getFontResolver()
31
    {
32
        return $this->fontResolver;
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    protected function getFontFace()
39
    {
40
        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...
41
    }
42
43
    /**
44
     * @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...
45
     */
46
    protected function getFontSize()
47
    {
48
        return $this->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\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...
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    protected function getText()
55
    {
56
        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...
57
    }
58
59
    /**
60
     * @return int
61
     */
62
    protected function getOrientation()
63
    {
64
        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...
65
    }
66
67
    /**
68
     * @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...
69
     */
70 1
    public function render()
71
    {
72 1
        $fontResolver = $this->getFontResolver();
73
74 1
        $fontPath = $fontResolver($this->getFontFace());
75
76
        // create the text here so it can be measured
77 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...
78
79 1
        $fontOverride->file($fontPath);
80 1
        $fontOverride->size($this->getFontSize());
81 1
        $fontOverride->color('#000');
82 1
        $fontOverride->valign('top');
83
84 1
        $size = $fontOverride->getBoxSize();
85
86 1
        $fontCallback = function (&$font) use ($fontOverride) {
87
            // override the font created by Canvas::text()
88 1
            $font = $fontOverride;
89 1
        };
90
91 1
        $canvas = $this->getImageManager()->canvas($size['width'], $size['height'], $this->getTracerColour());
92
93 1
        $canvas->text('', null, null, $fontCallback);
94
95 1
        $canvas->resize($size['width'] * self::LETTER_SPACING_ADJUSTMENT, $size['height']);
96
97 1
        if ($this->getOrientation()) {
98 1
            $canvas->rotate(360 - $this->getOrientation());
99
        }
100
101 1
        return $canvas;
102
    }
103
}
104