Completed
Push — master ( 4dc0e8...439184 )
by Hannes
10s
created

Fpdi2Driver::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace iio\libmergepdf\Driver;
6
7
use iio\libmergepdf\Exception;
8
use iio\libmergepdf\Source\SourceInterface;
9
use setasign\Fpdi\Fpdi as FpdiFpdf;
10
use setasign\Fpdi\Tcpdf\Fpdi as FpdiTcpdf;
11
use setasign\Fpdi\PdfParser\StreamReader;
12
13
final class Fpdi2Driver implements DriverInterface
14
{
15
    /**
16
     * @var FpdiFpdf|FpdiTcpdf
17
     */
18
    private $fpdi;
19
20
    /**
21
     * @param FpdiFpdf|FpdiTcpdf $fpdi
22
     */
23
    public function __construct($fpdi = null)
24
    {
25
        $this->fpdi = $fpdi ?: new FpdiTcpdf;
26
27
        if (!($this->fpdi instanceof FpdiFpdf) && !($this->fpdi instanceof FpdiTcpdf)) {
28
            throw new \InvalidArgumentException('Constructor argument must be an FPDI instance.');
29
        }
30
    }
31
32 View Code Duplication
    public function merge(SourceInterface ...$sources): string
0 ignored issues
show
Duplication introduced by
This method 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...
33
    {
34
        $sourceName = '';
35
36
        try {
37
            $fpdi = clone $this->fpdi;
38
39
            foreach ($sources as $source) {
40
                $sourceName = $source->getName();
41
                $pageCount = $fpdi->setSourceFile(StreamReader::createByString($source->getContents()));
42
                $pageNumbers = $source->getPages()->getPageNumbers() ?: range(1, $pageCount);
43
44
                foreach ($pageNumbers as $pageNr) {
45
                    $template = $fpdi->importPage($pageNr);
46
                    $size = $fpdi->getTemplateSize($template);
47
                    $fpdi->AddPage(
0 ignored issues
show
Bug introduced by
The method AddPage does only exist in setasign\Fpdi\Fpdi, but not in setasign\Fpdi\Tcpdf\Fpdi.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
                        $size['width'] > $size['height'] ? 'L' : 'P',
49
                        [$size['width'], $size['height']]
50
                    );
51
                    $fpdi->useTemplate($template);
52
                }
53
            }
54
55
            return $fpdi->Output('', 'S');
56
        } catch (\Exception $e) {
57
            throw new Exception("'{$e->getMessage()}' in '$sourceName'", 0, $e);
58
        }
59
    }
60
}
61