Skip to content

Commit d8d1b87

Browse files
Add PHPStan, fix violations (#799)
* Add phpstan, compliant with level 2 * Compliant with level 3 * Compliant with level 5 * Add PHPStan to composer, grumphp, and github actions * Compliant with level 8
1 parent 7a26d36 commit d8d1b87

9 files changed

+111
-85
lines changed

.github/workflows/run-tests.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@ jobs:
4747
composer require "laravel/framework:${{ matrix.laravel }}" --no-update --no-progress
4848
composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress
4949
50+
- name: Run Static Analysis
51+
run: composer phpstan
52+
5053
- name: Execute Unit Tests
51-
run: composer test
54+
run: composer test

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"require-dev": {
1818
"orchestra/testbench": "^4|^5|^6",
1919
"squizlabs/php_codesniffer": "^3.5",
20-
"phpro/grumphp": "^1"
20+
"phpro/grumphp": "^1",
21+
"nunomaduro/larastan": "^0.7.10"
2122
},
2223
"autoload": {
2324
"psr-4": {
@@ -45,7 +46,8 @@
4546
"scripts": {
4647
"test": "phpunit",
4748
"check-style": "phpcs -p --standard=psr12 src/",
48-
"fix-style": "phpcbf -p --standard=psr12 src/"
49+
"fix-style": "phpcbf -p --standard=psr12 src/",
50+
"phpstan": "phpstan analyze --memory-limit=-1"
4951
},
5052
"minimum-stability": "dev",
5153
"prefer-stable": true

grumphp.yml

+9
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ grumphp:
1111
ignore_patterns:
1212
- tests/
1313
triggered_by: [php]
14+
phpstan:
15+
autoload_file: ~
16+
configuration: ~
17+
level: null
18+
force_patterns: [ ]
19+
ignore_patterns: [ ]
20+
triggered_by: [ 'php' ]
21+
memory_limit: "-1"
22+
use_grumphp_paths: true

phpstan.neon

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
includes:
2+
- vendor/nunomaduro/larastan/extension.neon
3+
4+
parameters:
5+
paths:
6+
- src
7+
- tests
8+
level: 8
9+
ignoreErrors:
10+
# This is a global alias that cannot be detected by Larastan.
11+
- '#Call to static method loadHtml\(\) on an unknown class PDF\.#'

src/Facade.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ protected static function getFacadeAccessor()
3333

3434
/**
3535
* Resolve a new instance
36+
* @param string $method
37+
* @param array<mixed> $args
38+
* @return mixed
3639
*/
3740
public static function __callStatic($method, $args)
3841
{
@@ -55,7 +58,11 @@ public static function __callStatic($method, $args)
5558
return $instance->$method($args[0], $args[1], $args[2], $args[3]);
5659

5760
default:
58-
return call_user_func_array(array($instance, $method), $args);
61+
$callable = [$instance, $method];
62+
if (! is_callable($callable)) {
63+
throw new \UnexpectedValueException("Method PDF::{$method}() does not exist.");
64+
}
65+
return call_user_func_array($callable, $args);
5966
}
6067
}
6168
}

src/PDF.php

+46-66
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Barryvdh\DomPDF;
44

5+
use Dompdf\Adapter\CPDF;
56
use Dompdf\Dompdf;
67
use Dompdf\Options;
78
use Exception;
@@ -31,8 +32,13 @@ class PDF
3132
/** @var \Illuminate\Contracts\View\Factory */
3233
protected $view;
3334

35+
/** @var bool */
3436
protected $rendered = false;
37+
38+
/** @var bool */
3539
protected $showWarnings;
40+
41+
/** @var string */
3642
protected $public_path;
3743

3844
/**
@@ -56,31 +62,26 @@ public function __construct(Dompdf $dompdf, ConfigRepository $config, Filesystem
5662
*
5763
* @return Dompdf
5864
*/
59-
public function getDomPDF()
65+
public function getDomPDF(): Dompdf
6066
{
6167
return $this->dompdf;
6268
}
6369

6470
/**
6571
* Set the paper size (default A4)
6672
*
67-
* @param string|array $paper
68-
* @param string $orientation
69-
* @return $this
73+
* @param string|array<string> $paper
7074
*/
71-
public function setPaper($paper, $orientation = 'portrait')
75+
public function setPaper($paper, string $orientation = 'portrait'): self
7276
{
7377
$this->dompdf->setPaper($paper, $orientation);
7478
return $this;
7579
}
7680

7781
/**
7882
* Show or hide warnings
79-
*
80-
* @param bool $warnings
81-
* @return $this
8283
*/
83-
public function setWarnings($warnings)
84+
public function setWarnings(bool $warnings): self
8485
{
8586
$this->showWarnings = $warnings;
8687
return $this;
@@ -89,11 +90,9 @@ public function setWarnings($warnings)
8990
/**
9091
* Load a HTML string
9192
*
92-
* @param string $string
93-
* @param string $encoding Not used yet
94-
* @return static
93+
* @param string|null $encoding Not used yet
9594
*/
96-
public function loadHTML($string, $encoding = null)
95+
public function loadHTML(string $string, ?string $encoding = null): self
9796
{
9897
$string = $this->convertEntities($string);
9998
$this->dompdf->loadHtml($string, $encoding);
@@ -103,11 +102,8 @@ public function loadHTML($string, $encoding = null)
103102

104103
/**
105104
* Load a HTML file
106-
*
107-
* @param string $file
108-
* @return static
109105
*/
110-
public function loadFile($file)
106+
public function loadFile(string $file): self
111107
{
112108
$this->dompdf->loadHtmlFile($file);
113109
$this->rendered = false;
@@ -116,11 +112,10 @@ public function loadFile($file)
116112

117113
/**
118114
* Add metadata info
119-
*
120-
* @param array $info
115+
* @param array<string, string> $info
121116
* @return static
122117
*/
123-
public function addInfo($info)
118+
public function addInfo(array $info): self
124119
{
125120
foreach ($info as $name => $value) {
126121
$this->dompdf->add_info($name, $value);
@@ -130,14 +125,11 @@ public function addInfo($info)
130125

131126
/**
132127
* Load a View and convert to HTML
133-
*
134-
* @param string $view
135-
* @param array $data
136-
* @param array $mergeData
137-
* @param string $encoding Not used yet
138-
* @return static
128+
* @param array<string, mixed> $data
129+
* @param array<string, mixed> $mergeData
130+
* @param string|null $encoding Not used yet
139131
*/
140-
public function loadView($view, $data = array(), $mergeData = array(), $encoding = null)
132+
public function loadView(string $view, array $data = [], array $mergeData = [], ?string $encoding = null): self
141133
{
142134
$html = $this->view->make($view, $data, $mergeData)->render();
143135
return $this->loadHTML($html, $encoding);
@@ -146,10 +138,9 @@ public function loadView($view, $data = array(), $mergeData = array(), $encoding
146138
/**
147139
* Set/Change an option in DomPdf
148140
*
149-
* @param array $options
150-
* @return static
141+
* @param array<string, mixed> $options
151142
*/
152-
public function setOptions(array $options)
143+
public function setOptions(array $options): self
153144
{
154145
$options = new Options($options);
155146
$this->dompdf->setOptions($options);
@@ -164,70 +155,57 @@ public function setOptions(array $options)
164155
* 'compress' = > 1 or 0 - apply content stream compression, this is
165156
* on (1) by default
166157
*
167-
* @param array $options
158+
* @param array<string, int> $options
168159
*
169160
* @return string The rendered PDF as string
170161
*/
171-
public function output($options = [])
162+
public function output(array $options = []): string
172163
{
173164
if (!$this->rendered) {
174165
$this->render();
175166
}
176-
return $this->dompdf->output($options);
167+
return (string) $this->dompdf->output($options);
177168
}
178169

179170
/**
180171
* Save the PDF to a file
181-
*
182-
* @param $filename
183-
* @return static
184172
*/
185-
public function save($filename)
173+
public function save(string $filename): self
186174
{
187175
$this->files->put($filename, $this->output());
188176
return $this;
189177
}
190178

191179
/**
192180
* Make the PDF downloadable by the user
193-
*
194-
* @param string $filename
195-
* @return \Illuminate\Http\Response
196181
*/
197-
public function download($filename = 'document.pdf')
182+
public function download(string $filename = 'document.pdf'): Response
198183
{
199184
$output = $this->output();
200-
return new Response($output, 200, array(
201-
'Content-Type' => 'application/pdf',
202-
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
203-
'Content-Length' => strlen($output),
204-
));
185+
return new Response($output, 200, [
186+
'Content-Type' => 'application/pdf',
187+
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
188+
'Content-Length' => strlen($output),
189+
]);
205190
}
206191

207192
/**
208193
* Return a response with the PDF to show in the browser
209-
*
210-
* @param string $filename
211-
* @return \Illuminate\Http\Response
212194
*/
213-
public function stream($filename = 'document.pdf')
195+
public function stream(string $filename = 'document.pdf'): Response
214196
{
215197
$output = $this->output();
216-
return new Response($output, 200, array(
198+
return new Response($output, 200, [
217199
'Content-Type' => 'application/pdf',
218200
'Content-Disposition' => 'inline; filename="' . $filename . '"',
219-
));
201+
]);
220202
}
221203

222204
/**
223205
* Render the PDF
224206
*/
225-
public function render()
207+
public function render(): void
226208
{
227-
if (!$this->dompdf) {
228-
throw new Exception('DOMPDF not created yet');
229-
}
230-
231209
$this->dompdf->render();
232210

233211
if ($this->showWarnings) {
@@ -246,25 +224,27 @@ public function render()
246224
$this->rendered = true;
247225
}
248226

249-
public function setEncryption($password, $ownerpassword = '', $pc = [])
227+
/** @param array<string> $pc */
228+
public function setEncryption(string $password, string $ownerpassword = '', array $pc = []): void
250229
{
251-
if (!$this->dompdf) {
252-
throw new Exception("DOMPDF not created yet");
253-
}
254-
$this->render();
255-
return $this->dompdf->getCanvas()->get_cpdf()->setEncryption($password, $ownerpassword, $pc);
230+
$this->render();
231+
$canvas = $this->dompdf->getCanvas();
232+
if (! $canvas instanceof CPDF) {
233+
throw new \RuntimeException('Encryption is only supported when using CPDF');
234+
}
235+
$canvas->get_cpdf()->setEncryption($password, $ownerpassword, $pc);
256236
}
257237

258-
protected function convertEntities($subject)
238+
protected function convertEntities(string $subject): string
259239
{
260240
if (false === $this->config->get('dompdf.convert_entities', true)) {
261241
return $subject;
262242
}
263243

264-
$entities = array(
244+
$entities = [
265245
'' => '&euro;',
266246
'£' => '&pound;',
267-
);
247+
];
268248

269249
foreach ($entities as $search => $replace) {
270250
$subject = str_replace($search, $replace, $subject);

src/ServiceProvider.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ServiceProvider extends IlluminateServiceProvider
2323
* @throws \Exception
2424
* @return void
2525
*/
26-
public function register()
26+
public function register(): void
2727
{
2828
$configPath = __DIR__ . '/../config/dompdf.php';
2929
$this->mergeConfigFrom($configPath, 'dompdf');
@@ -33,6 +33,10 @@ public function register()
3333

3434
if ($defines) {
3535
$options = [];
36+
/**
37+
* @var string $key
38+
* @var mixed $value
39+
*/
3640
foreach ($defines as $key => $value) {
3741
$key = strtolower(str_replace('DOMPDF_', '', $key));
3842
$options[$key] = $value;
@@ -48,7 +52,11 @@ public function register()
4852

4953
$options = $app->make('dompdf.options');
5054
$dompdf = new Dompdf($options);
51-
$dompdf->setBasePath(realpath(base_path('public')));
55+
$path = realpath(base_path('public'));
56+
if ($path === false) {
57+
throw new \RuntimeException('Cannot resolve public path');
58+
}
59+
$dompdf->setBasePath($path);
5260

5361
return $dompdf;
5462
});
@@ -61,15 +69,13 @@ public function register()
6169

6270
/**
6371
* Check if package is running under Lumen app
64-
*
65-
* @return bool
6672
*/
67-
protected function isLumen()
73+
protected function isLumen(): bool
6874
{
6975
return Str::contains($this->app->version(), 'Lumen') === true;
7076
}
7177

72-
public function boot()
78+
public function boot(): void
7379
{
7480
if (! $this->isLumen()) {
7581
$configPath = __DIR__ . '/../config/dompdf.php';
@@ -80,10 +86,10 @@ public function boot()
8086
/**
8187
* Get the services provided by the provider.
8288
*
83-
* @return array
89+
* @return array<string>
8490
*/
85-
public function provides()
91+
public function provides(): array
8692
{
87-
return array('dompdf', 'dompdf.options', 'dompdf.wrapper');
93+
return ['dompdf', 'dompdf.options', 'dompdf.wrapper'];
8894
}
8995
}

0 commit comments

Comments
 (0)