Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 6c40590

Browse files
committed
Chore: Coding standards refactor
1 parent 77d32b6 commit 6c40590

13 files changed

+168
-60
lines changed

src/PHPImageOptim/PHPImageOptim.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ public function setImage(string $imagePath = ''): PHPImageOptim
3232
}
3333

3434
/**
35-
* @param $object
35+
* @param mixed $object
36+
* @param bool $stopIfFail
3637
* @return PHPImageOptim
3738
*/
38-
public function chainCommand($object, $stopIfFail = true): PHPImageOptim
39+
public function chainCommand($object, bool $stopIfFail = true): PHPImageOptim
3940
{
4041
$object->setStopIfFail($stopIfFail);
4142
$this->chainedCommands[] = $object;

src/PHPImageOptim/Tools/Common.php

+57-17
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,47 @@
66

77
class Common
88
{
9+
/**
10+
* @var string
11+
*/
912
protected $binaryPath = '';
13+
14+
/**
15+
* @var string
16+
*/
1017
protected $imagePath = '';
18+
19+
/**
20+
* @var string
21+
*/
1122
protected $outputPath = '';
12-
protected $originalFileSize = '';
13-
protected $finalFileSize = '';
23+
24+
/**
25+
* @var int
26+
*/
27+
protected $originalFileSize = 0;
28+
29+
/**
30+
* @var int
31+
*/
32+
protected $finalFileSize = 0;
33+
34+
/**
35+
* @var int
36+
*/
1437
protected $optimisationLevel = 1;
38+
39+
/**
40+
* @var bool
41+
*/
1542
protected $stopIfFail = true;
1643

1744
/**
1845
* @param string $binaryPath
1946
* @return $this
2047
* @throws Exception
2148
*/
22-
public function setBinaryPath($binaryPath = '')
49+
public function setBinaryPath(string $binaryPath = ''): Common
2350
{
2451
if (!file_exists($binaryPath)) {
2552
throw new Exception('Unable to locate binary file');
@@ -30,22 +57,21 @@ public function setBinaryPath($binaryPath = '')
3057
}
3158

3259
/**
33-
* @param string $stopIfFail
60+
* @param bool $stopIfFail
3461
* @return $this
35-
* @throws Exception
3662
*/
37-
public function setStopIfFail($stopIfFail)
63+
public function setStopIfFail(bool $stopIfFail): Common
3864
{
39-
$this->stopIfFail = boolval($stopIfFail);
65+
$this->stopIfFail = $stopIfFail;
4066
return $this;
4167
}
4268

4369
/**
44-
* @param $imagePath
45-
* @return $this
70+
* @param string $imagePath
71+
* @return Common
4672
* @throws Exception
4773
*/
48-
public function setImagePath($imagePath)
74+
public function setImagePath(string $imagePath): Common
4975
{
5076
if (!file_exists($imagePath)) {
5177
throw new Exception('Invald image path');
@@ -64,7 +90,7 @@ public function setImagePath($imagePath)
6490
* @return $this
6591
* @throws Exception
6692
*/
67-
public function setOptimisationLevel($level = 2)
93+
public function setOptimisationLevel(int $level = 2): Common
6894
{
6995
if (!is_int($level)) {
7096
throw new Exception('Invalid Optimisation Level');
@@ -82,20 +108,34 @@ public function setOptimisationLevel($level = 2)
82108
}
83109

84110
/**
85-
* @return $this
111+
* @return Common
112+
* @throws Exception
86113
*/
87-
public function determinePreOptimisedFileSize()
114+
public function determinePreOptimisedFileSize(): Common
88115
{
89-
$this->originalFileSize = filesize($this->imagePath);
116+
$fileSize = filesize($this->imagePath);
117+
118+
if ($fileSize === false) {
119+
throw new \Exception('Unable to determine pre-optimised fileSize');
120+
}
121+
122+
$this->originalFileSize = $fileSize;
90123
return $this;
91124
}
92125

93126
/**
94-
* @return $this
127+
* @return Common
128+
* @throws Exception
95129
*/
96-
public function determinePostOptimisedFileSize()
130+
public function determinePostOptimisedFileSize(): Common
97131
{
98-
$this->finalFileSize = filesize($this->imagePath);
132+
$fileSize = filesize($this->imagePath);
133+
134+
if ($fileSize === false) {
135+
throw new \Exception('Unable to determine post-optimised fileSize');
136+
}
137+
138+
$this->finalFileSize = $fileSize;
99139
return $this;
100140
}
101141
}

src/PHPImageOptim/Tools/Gif/Gifsicle.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ class Gifsicle extends Common implements ToolsInterface
1414
*/
1515
public function optimise(): ToolsInterface
1616
{
17-
exec($this->binaryPath . ' -b -O2 ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
18-
if ($this->stopIfFail && $iResult !== 0) {
19-
throw new Exception('GIFSICLE was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
17+
exec(
18+
$this->binaryPath . ' -b -O2 ' . escapeshellarg($this->imagePath),
19+
$output,
20+
$optimResult
21+
);
22+
if ($this->stopIfFail && $optimResult !== 0) {
23+
throw new Exception('GIFSICLE was unable to optimise image, result:' . $optimResult . ' File: ' . $this->imagePath);
2024
}
2125

2226
return $this;

src/PHPImageOptim/Tools/Jpeg/Guetzli.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public function optimise(): ToolsInterface
4141
escapeshellarg($this->imagePath),
4242
escapeshellarg($this->imagePath)
4343
),
44-
$aOutput,
45-
$iResult
44+
$output,
45+
$optimResult
4646
);
4747

48-
if ($this->stopIfFail && $iResult !== 0) {
49-
throw new Exception('GUETZLI was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
48+
if ($this->stopIfFail && $optimResult !== 0) {
49+
throw new Exception('GUETZLI was unable to optimise image, result:' . $optimResult . ' File: ' . $this->imagePath);
5050
}
5151
return $this;
5252
}

src/PHPImageOptim/Tools/Jpeg/JpegOptim.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ class JpegOptim extends Common implements ToolsInterface
1414
*/
1515
public function optimise(): ToolsInterface
1616
{
17-
exec($this->binaryPath . ' --strip-all --all-progressive ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
18-
if ($this->stopIfFail && $iResult !== 0) {
19-
throw new Exception('JPEGOPTIM was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
17+
exec(
18+
$this->binaryPath . ' --strip-all --all-progressive ' . escapeshellarg($this->imagePath),
19+
$output,
20+
$optimResult
21+
);
22+
23+
if ($this->stopIfFail && $optimResult !== 0) {
24+
throw new Exception('JPEGOPTIM was unable to optimise image, result:' . $optimResult . ' File: ' . $this->imagePath);
2025
}
2126

2227
return $this;

src/PHPImageOptim/Tools/Jpeg/JpegTran.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ class JpegTran extends Common implements ToolsInterface
1414
*/
1515
public function optimise(): ToolsInterface
1616
{
17-
exec($this->binaryPath . ' -optimize ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
18-
if ($this->stopIfFail && $iResult !== 0) {
19-
throw new Exception('JPEGTRAN was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
17+
exec(
18+
$this->binaryPath . ' -optimize ' . escapeshellarg($this->imagePath),
19+
$output,
20+
$optimResult
21+
);
22+
23+
if ($this->stopIfFail && $optimResult !== 0) {
24+
throw new Exception('JPEGTRAN was unable to optimise image, result:' . $optimResult . ' File: ' . $this->imagePath);
2025
}
2126

2227
return $this;

src/PHPImageOptim/Tools/Jpeg/MozJpeg.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class MozJpeg extends Common implements ToolsInterface
1010
{
11+
/**
12+
* @var string
13+
*/
1114
private $attributes = '';
1215

1316
/**
@@ -32,24 +35,26 @@ public function __construct(array $options = ['quality' => 85])
3235
public function optimise(): ToolsInterface
3336
{
3437
$tempFile = tempnam(sys_get_temp_dir(), 'PHPImageOptim');
38+
39+
if ($tempFile === false) {
40+
throw new Exception('Unable to create temp file for PHPImageOptim');
41+
}
42+
3543
$command = sprintf(
3644
$this->binaryPath . ' %s -outfile %s %s',
3745
$this->attributes,
3846
escapeshellarg($tempFile),
3947
escapeshellarg($this->imagePath)
4048
);
4149

42-
exec(
43-
$command,
44-
$aOutput,
45-
$iResult
46-
);
50+
exec($command, $output, $result);
4751

4852
rename($tempFile, $this->imagePath);
4953

50-
if ($this->stopIfFail && $iResult !== 0) {
51-
throw new Exception('MOZJPEG was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
54+
if ($this->stopIfFail && $result !== 0) {
55+
throw new Exception('MOZJPEG was unable to optimise image, result:' . $result . ' File: ' . $this->imagePath);
5256
}
57+
5358
return $this;
5459
}
5560

src/PHPImageOptim/Tools/Png/AdvPng.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ class AdvPng extends Common implements ToolsInterface
1414
*/
1515
public function optimise(): ToolsInterface
1616
{
17-
exec($this->binaryPath . ' -z -4 -i20 -- ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
18-
if ($this->stopIfFail && $iResult !== 0) {
19-
throw new Exception('ADVPNG was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
17+
exec(
18+
$this->binaryPath . ' -z -4 -i20 -- ' . escapeshellarg($this->imagePath),
19+
$output,
20+
$result
21+
);
22+
23+
if ($this->stopIfFail && $result !== 0) {
24+
throw new Exception('ADVPNG was unable to optimise image, result:' . $result . ' File: ' . $this->imagePath);
2025
}
2126

2227
return $this;

src/PHPImageOptim/Tools/Png/OptiPng.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ class OptiPng extends Common implements ToolsInterface
1414
*/
1515
public function optimise(): ToolsInterface
1616
{
17-
exec($this->binaryPath . ' -i0 -o7 -zm1-9 ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
18-
if ($this->stopIfFail && $iResult !== 0) {
19-
throw new Exception('OPTIPNG was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
17+
exec(
18+
$this->binaryPath . ' -i0 -o7 -zm1-9 ' . escapeshellarg($this->imagePath),
19+
$output,
20+
$optimResult
21+
);
22+
23+
if ($this->stopIfFail && $optimResult !== 0) {
24+
throw new Exception('OPTIPNG was unable to optimise image, result:' . $optimResult . ' File: ' . $this->imagePath);
2025
}
2126

2227
return $this;

src/PHPImageOptim/Tools/Png/PngCrush.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,31 @@ class PngCrush extends Common implements ToolsInterface
1515
public function optimise(): ToolsInterface
1616
{
1717
$absoluteImagePath = realpath($this->imagePath);
18+
if ($absoluteImagePath === false) {
19+
throw new \Exception('Unable to escape PngCrush image path');
20+
}
21+
1822
// Write file to temporary location
19-
$prevDir = getcwd();
23+
$currentDirectory = getcwd();
24+
if ($currentDirectory === false) {
25+
throw new \Exception('Unable to get current working folder');
26+
}
27+
2028
chdir(sys_get_temp_dir());
2129

22-
exec($this->binaryPath . ' -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -q -l 9 -reduce -ow ' . escapeshellarg($absoluteImagePath), $aOutput, $iResult);
30+
exec(
31+
$this->binaryPath . ' -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -q -l 9 -reduce -ow ' . escapeshellarg($absoluteImagePath),
32+
$aOutput,
33+
$optimResult
34+
);
2335

2436
// Switch back to previous directory
25-
chdir($prevDir);
37+
if (chdir($currentDirectory) === false) {
38+
throw new \Exception('Unable to change folder');
39+
}
2640

27-
if ($this->stopIfFail && $iResult != 0) {
28-
throw new Exception('PNGCRUSH was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
41+
if ($this->stopIfFail && $optimResult != 0) {
42+
throw new Exception('PNGCRUSH was unable to optimise image, result:' . $optimResult . ' File: ' . $this->imagePath);
2943
}
3044

3145
return $this;

src/PHPImageOptim/Tools/Png/PngOut.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ class PngOut extends Common implements ToolsInterface
1414
*/
1515
public function optimise(): ToolsInterface
1616
{
17-
exec($this->binaryPath . ' ' . $this->getOptimisationLevel() . ' -q -y ' . escapeshellarg($this->imagePath) . ' ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
17+
exec(
18+
$this->binaryPath . ' ' . $this->getOptimisationLevel() . ' -q -y ' . escapeshellarg($this->imagePath) . ' ' . escapeshellarg($this->imagePath),
19+
$output,
20+
$optimResult
21+
);
1822

19-
if ($iResult === 2) {
23+
if ($optimResult === 2) {
2024
return $this;
2125
}
2226

23-
if ($this->stopIfFail && $iResult !== 0) {
24-
throw new Exception('PNGOUT was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
27+
if ($this->stopIfFail && $optimResult !== 0) {
28+
throw new Exception('PNGOUT was unable to optimise image, result:' . $optimResult . ' File: ' . $this->imagePath);
2529
}
2630

2731
return $this;

src/PHPImageOptim/Tools/Png/PngQuant.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ class PngQuant extends Common implements ToolsInterface
1414
*/
1515
public function optimise(): ToolsInterface
1616
{
17-
exec($this->binaryPath . ' --speed 1 --ext=.png --force ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
18-
if ($this->stopIfFail && $iResult !== 0) {
19-
throw new Exception('PNGQUANT was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
17+
exec(
18+
$this->binaryPath . ' --speed 1 --ext=.png --force ' . escapeshellarg($this->imagePath),
19+
$output,
20+
$optimResult
21+
);
22+
23+
if ($this->stopIfFail && $optimResult !== 0) {
24+
throw new Exception('PNGQUANT was unable to optimise image, result:' . $optimResult . ' File: ' . $this->imagePath);
2025
}
2126

2227
return $this;

0 commit comments

Comments
 (0)