GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0a1867...b18af9 )
by Freek
9s
created

Pdf::setOptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace Spatie\PdfToText;
4
5
use Spatie\PdfToText\Exceptions\CouldNotExtractText;
6
use Spatie\PdfToText\Exceptions\PdfNotFound;
7
use Symfony\Component\Process\Process;
8
9
class Pdf
10
{
11
    protected $pdf;
12
13
    protected $binPath;
14
15
    protected $options = [];
16
17
    public function __construct(string $binPath = null)
18
    {
19
        $this->binPath = $binPath ?? '/usr/bin/pdftotext';
20
    }
21
22
    public function setPdf(string $pdf) : self
23
    {
24
        if (!is_readable($pdf)) {
25
            throw new PdfNotFound(sprintf('could not find or read pdf `%s`', $pdf));
26
        }
27
28
        $this->pdf = $pdf;
29
30
        return $this;
31
    }
32
33
    public function setOptions(array $options) : self
34
    {
35
        $mapper = function (string $content) : array {
36
            $content = trim($content);
37
            if ('-' !== ($content[0] ?? '')) {
38
                $content = '-'.$content;
39
            }
40
41
            return explode(' ', $content, 2);
42
        };
43
44
        $reducer = function (array $carry, array $option) : array {
45
            return array_merge($carry, $option);
46
        };
47
48
        $this->options = array_reduce(array_map($mapper, $options), $reducer, []);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_reduce(array_map($...ns), $reducer, array()) of type * is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
50
        return $this;
51
    }
52
53
    public function text() : string
54
    {
55
        $process = new Process(array_merge([$this->binPath], $this->options, [$this->pdf, '-']));
56
        $process->run();
57
        if (!$process->isSuccessful()) {
58
            throw new CouldNotExtractText($process);
59
        }
60
61
        return trim($process->getOutput(), " \t\n\r\0\x0B\x0C");
62
    }
63
64
    public static function getText(string $pdf, string $binPath = null, array $options = []) : string
65
    {
66
        return (new static($binPath))
67
            ->setOptions($options)
68
            ->setPdf($pdf)
69
            ->text()
70
        ;
71
    }
72
}
73