Completed
Push — master ( 4dc0e8...439184 )
by Hannes
22s queued 18s
created

src/Driver/Fpdi2Driver.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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