Skip to content

Commit bfc651d

Browse files
qwerty287ildyria
andauthored
Remove unnecessary exception and null checks (#63)
* Remove unnecessary exception and `null` checks * Remove bad `@covers` annotations * Remove all `@covers` * Fix test * Remove useless function alias * Update lib/PHPExif/Reader/Reader.php Co-authored-by: Benoît Viguier <[email protected]> * New exceptions * fix constructor * fix format * add codecov ignore --------- Co-authored-by: Benoît Viguier <[email protected]>
1 parent 8460af5 commit bfc651d

30 files changed

+409
-864
lines changed

lib/PHPExif/Adapter/AbstractAdapter.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(array $options = array())
4343
* @param \PHPExif\Contracts\MapperInterface $mapper
4444
* @return \PHPExif\Contracts\AdapterInterface
4545
*/
46-
public function setMapper(MapperInterface $mapper) : AdapterInterface
46+
public function setMapper(MapperInterface $mapper): AdapterInterface
4747
{
4848
$this->mapper = $mapper;
4949

@@ -55,12 +55,12 @@ public function setMapper(MapperInterface $mapper) : AdapterInterface
5555
*
5656
* @return \PHPExif\Contracts\MapperInterface
5757
*/
58-
public function getMapper() : MapperInterface
58+
public function getMapper(): MapperInterface
5959
{
6060
if (null === $this->mapper) {
6161
// lazy load one
6262
/** @var MapperInterface */
63-
$mapper = new $this->mapperClass;
63+
$mapper = new $this->mapperClass();
6464

6565
$this->setMapper($mapper);
6666
}
@@ -74,7 +74,7 @@ public function getMapper() : MapperInterface
7474
* @param \PHPExif\Contracts\HydratorInterface $hydrator
7575
* @return \PHPExif\Contracts\AdapterInterface
7676
*/
77-
public function setHydrator(HydratorInterface $hydrator) : AdapterInterface
77+
public function setHydrator(HydratorInterface $hydrator): AdapterInterface
7878
{
7979
$this->hydrator = $hydrator;
8080

@@ -86,12 +86,12 @@ public function setHydrator(HydratorInterface $hydrator) : AdapterInterface
8686
*
8787
* @return \PHPExif\Contracts\HydratorInterface
8888
*/
89-
public function getHydrator() : HydratorInterface
89+
public function getHydrator(): HydratorInterface
9090
{
9191
if (null === $this->hydrator) {
9292
// lazy load one
9393
/** @var HydratorInterface */
94-
$hydrator = new $this->hydratorClass;
94+
$hydrator = new $this->hydratorClass();
9595

9696
$this->setHydrator($hydrator);
9797
}
@@ -105,7 +105,7 @@ public function getHydrator() : HydratorInterface
105105
* @param array $options
106106
* @return \PHPExif\Contracts\AdapterInterface
107107
*/
108-
public function setOptions(array $options) : AdapterInterface
108+
public function setOptions(array $options): AdapterInterface
109109
{
110110
$hydrator = $this->getHydrator();
111111
$hydrator->hydrate($this, $options);
@@ -122,7 +122,7 @@ public function setOptions(array $options) : AdapterInterface
122122
*/
123123
// @codeCoverageIgnoreStart
124124
// this is fine because we use it directly in our tests for Exiftool and Native
125-
public function convertToUTF8(array|string $data) : array|string
125+
public function convertToUTF8(array|string $data): array|string
126126
{
127127
if (is_array($data)) {
128128
/** @var array|string|null $v */

lib/PHPExif/Adapter/Exiftool.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use PHPExif\Exif;
66
use InvalidArgumentException;
77
use PHPExif\Mapper\Exiftool as MapperExiftool;
8-
use RuntimeException;
8+
use PHPExif\Reader\PhpExifReaderException;
99
use Safe\Exceptions\ExecException;
1010

11+
use Safe\Exceptions\JsonException;
12+
1113
use function Safe\exec;
1214
use function Safe\json_decode;
1315
use function Safe\stream_get_contents;
@@ -23,7 +25,7 @@
2325
*/
2426
class Exiftool extends AbstractAdapter
2527
{
26-
const TOOL_NAME = 'exiftool';
28+
public const TOOL_NAME = 'exiftool';
2729

2830
/**
2931
* Path to the exiftool binary
@@ -40,7 +42,7 @@ class Exiftool extends AbstractAdapter
4042
* @return \PHPExif\Adapter\Exiftool Current instance
4143
* @throws \InvalidArgumentException When path is invalid
4244
*/
43-
public function setToolPath(string $path) : Exiftool
45+
public function setToolPath(string $path): Exiftool
4446
{
4547
if (!file_exists($path)) {
4648
throw new InvalidArgumentException(
@@ -59,7 +61,7 @@ public function setToolPath(string $path) : Exiftool
5961
/**
6062
* @param boolean $numeric
6163
*/
62-
public function setNumeric(bool $numeric) : void
64+
public function setNumeric(bool $numeric): void
6365
{
6466
$this->numeric = $numeric;
6567
}
@@ -68,7 +70,7 @@ public function setNumeric(bool $numeric) : void
6870
* @see http://www.sno.phy.queensu.ca/~phil/exiftool/faq.html#Q10
6971
* @param array $encodings encoding parameters in an array eg. ["exif" => "UTF-8"]
7072
*/
71-
public function setEncoding(array $encodings) : void
73+
public function setEncoding(array $encodings): void
7274
{
7375
$possible_keys = array("exif", "iptc", "id3", "photoshop", "quicktime",);
7476
$possible_values = array("UTF8", "cp65001", "UTF-8", "Thai", "cp874", "Latin", "cp1252",
@@ -90,7 +92,7 @@ public function setEncoding(array $encodings) : void
9092
*
9193
* @return string
9294
*/
93-
public function getToolPath() : string
95+
public function getToolPath(): string
9496
{
9597
if ($this->toolPath === '') {
9698
try {
@@ -109,10 +111,10 @@ public function getToolPath() : string
109111
* Reads & parses the EXIF data from given file
110112
*
111113
* @param string $file
112-
* @return \PHPExif\Exif Instance of Exif object with data
113-
* @throws \RuntimeException If the EXIF data could not be read
114+
* @return Exif Instance of Exif object with data
115+
* @throws PhpExifReaderException If the EXIF data could not be read
114116
*/
115-
public function getExifFromFile(string $file) : Exif
117+
public function getExifFromFile(string $file): Exif
116118
{
117119
$encoding = '';
118120
if (count($this->encoding) > 0) {
@@ -136,14 +138,20 @@ public function getExifFromFile(string $file) : Exif
136138
);
137139

138140
/**
139-
* @var string
141+
* @var string $result
140142
*/
141143
$result = $this->convertToUTF8($result);
142144

143-
$data = json_decode($result, true);
145+
try {
146+
$data = json_decode($result, true);
147+
} catch (JsonException $e) {
148+
// @codeCoverageIgnoreStart
149+
$data = false;
150+
// @codeCoverageIgnoreStart
151+
}
144152
if (!is_array($data)) {
145153
// @codeCoverageIgnoreStart
146-
throw new RuntimeException(
154+
throw new PhpExifReaderException(
147155
'Could not decode exiftool output'
148156
);
149157
// @codeCoverageIgnoreEnd
@@ -171,9 +179,9 @@ public function getExifFromFile(string $file) : Exif
171179
*
172180
* @param string $command
173181
* @return string|false
174-
* @throws RuntimeException If the command can't be executed
182+
* @throws PhpExifReaderException If the command can't be executed
175183
*/
176-
protected function getCliOutput(string $command) : string|false
184+
protected function getCliOutput(string $command): string|false
177185
{
178186
$descriptorspec = array(
179187
0 => array('pipe', 'r'),
@@ -184,7 +192,7 @@ protected function getCliOutput(string $command) : string|false
184192
$process = proc_open($command, $descriptorspec, $pipes);
185193

186194
if (!is_resource($process)) {
187-
throw new RuntimeException(
195+
throw new PhpExifReaderException(
188196
'Could not open a resource to the exiftool binary'
189197
);
190198
}

lib/PHPExif/Adapter/FFprobe.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use InvalidArgumentException;
1616
use FFMpeg;
1717
use PHPExif\Mapper\FFprobe as MapperFFprobe;
18-
use RuntimeException;
18+
use PHPExif\Reader\PhpExifReaderException;
1919
use Safe\Exceptions\ExecException;
2020

2121
use function Safe\exec;
@@ -113,7 +113,7 @@ public function getExifFromFile(string $file): Exif
113113

114114
// file is not a video -> wrong adapter
115115
if (strpos($mimeType, 'video') !== 0) {
116-
throw new RuntimeException('Could not read the video');
116+
throw new PhpExifReaderException('Could not read the video');
117117
}
118118

119119
$ffprobe = FFMpeg\FFProbe::create(array(

lib/PHPExif/Adapter/ImageMagick.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
class ImageMagick extends AbstractAdapter
2323
{
24-
const TOOL_NAME = 'imagick';
24+
public const TOOL_NAME = 'imagick';
2525

2626
protected string $mapperClass = MapperImageMagick::class;
2727

@@ -49,7 +49,7 @@ class ImageMagick extends AbstractAdapter
4949
* @param string $file
5050
* @return \PHPExif\Exif Instance of Exif object with data
5151
*/
52-
public function getExifFromFile(string $file) : Exif
52+
public function getExifFromFile(string $file): Exif
5353
{
5454
/* Create the object */
5555
$im = new Imagick($file);
@@ -98,7 +98,7 @@ public function getExifFromFile(string $file) : Exif
9898
* @param string $profile Raw IPTC data
9999
* @return array
100100
*/
101-
public function getIptcData(string $profile) : array
101+
public function getIptcData(string $profile): array
102102
{
103103
$arrData = [];
104104
try {

lib/PHPExif/Adapter/Native.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@
2121
*/
2222
class Native extends AbstractAdapter
2323
{
24-
const INCLUDE_THUMBNAIL = true;
25-
const NO_THUMBNAIL = false;
24+
public const INCLUDE_THUMBNAIL = true;
25+
public const NO_THUMBNAIL = false;
2626

27-
const SECTIONS_AS_ARRAYS = true;
28-
const SECTIONS_FLAT = false;
27+
public const SECTIONS_AS_ARRAYS = true;
28+
public const SECTIONS_FLAT = false;
2929

30-
const SECTION_FILE = 'FILE';
31-
const SECTION_COMPUTED = 'COMPUTED';
32-
const SECTION_IFD0 = 'IFD0';
33-
const SECTION_THUMBNAIL = 'THUMBNAIL';
34-
const SECTION_COMMENT = 'COMMENT';
35-
const SECTION_EXIF = 'EXIF';
36-
const SECTION_ALL = 'ANY_TAG';
37-
const SECTION_IPTC = 'IPTC';
30+
public const SECTION_FILE = 'FILE';
31+
public const SECTION_COMPUTED = 'COMPUTED';
32+
public const SECTION_IFD0 = 'IFD0';
33+
public const SECTION_THUMBNAIL = 'THUMBNAIL';
34+
public const SECTION_COMMENT = 'COMMENT';
35+
public const SECTION_EXIF = 'EXIF';
36+
public const SECTION_ALL = 'ANY_TAG';
37+
public const SECTION_IPTC = 'IPTC';
3838

3939
/**
4040
* List of EXIF sections
@@ -79,7 +79,7 @@ class Native extends AbstractAdapter
7979
*
8080
* @return array
8181
*/
82-
public function getRequiredSections() : array
82+
public function getRequiredSections(): array
8383
{
8484
return $this->requiredSections;
8585
}
@@ -90,7 +90,7 @@ public function getRequiredSections() : array
9090
* @param array $sections List of EXIF sections
9191
* @return \PHPExif\Adapter\Native Current instance for chaining
9292
*/
93-
public function setRequiredSections(array $sections) : Native
93+
public function setRequiredSections(array $sections): Native
9494
{
9595
$this->requiredSections = $sections;
9696

@@ -103,7 +103,7 @@ public function setRequiredSections(array $sections) : Native
103103
* @param string $section
104104
* @return \PHPExif\Adapter\Native Current instance for chaining
105105
*/
106-
public function addRequiredSection(string $section) : Native
106+
public function addRequiredSection(string $section): Native
107107
{
108108
if (!in_array($section, $this->requiredSections, true)) {
109109
array_push($this->requiredSections, $section);
@@ -118,7 +118,7 @@ public function addRequiredSection(string $section) : Native
118118
* @param boolean $value
119119
* @return \PHPExif\Adapter\Native Current instance for chaining
120120
*/
121-
public function setIncludeThumbnail(bool $value) : Native
121+
public function setIncludeThumbnail(bool $value): Native
122122
{
123123
$this->includeThumbnail = $value;
124124

@@ -130,7 +130,7 @@ public function setIncludeThumbnail(bool $value) : Native
130130
*
131131
* @return boolean
132132
*/
133-
public function getIncludeThumbnail() : bool
133+
public function getIncludeThumbnail(): bool
134134
{
135135
return $this->includeThumbnail;
136136
}
@@ -141,7 +141,7 @@ public function getIncludeThumbnail() : bool
141141
* @param boolean $value
142142
* @return \PHPExif\Adapter\Native Current instance for chaining
143143
*/
144-
public function setSectionsAsArrays(bool $value) : Native
144+
public function setSectionsAsArrays(bool $value): Native
145145
{
146146
$this->sectionsAsArrays = $value;
147147

@@ -153,7 +153,7 @@ public function setSectionsAsArrays(bool $value) : Native
153153
*
154154
* @return boolean
155155
*/
156-
public function getSectionsAsArrays() : bool
156+
public function getSectionsAsArrays(): bool
157157
{
158158
return $this->sectionsAsArrays;
159159
}
@@ -164,7 +164,7 @@ public function getSectionsAsArrays() : bool
164164
* @param string $file
165165
* @return \PHPExif\Exif Instance of Exif object with data
166166
*/
167-
public function getExifFromFile(string $file) : Exif
167+
public function getExifFromFile(string $file): Exif
168168
{
169169
$mimeType = mime_content_type($file);
170170

@@ -245,7 +245,7 @@ public function getExifFromFile(string $file) : Exif
245245
* @param string $file The file to read the IPTC data from
246246
* @return array
247247
*/
248-
public function getIptcData(string $file) : array
248+
public function getIptcData(string $file): array
249249
{
250250
getimagesize($file, $info);
251251
$arrData = array();

lib/PHPExif/Adapter/NoAdapterException.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/PHPExif/Contracts/AdapterInterface.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace PHPExif\Contracts;
77

88
use PHPExif\Exif;
9+
use PHPExif\Reader\PhpExifReaderException;
910

1011
/**
1112
* PHP Exif Reader Adapter
@@ -21,8 +22,8 @@ interface AdapterInterface
2122
* Reads & parses the EXIF data from given file
2223
*
2324
* @param string $file
24-
* @return \PHPExif\Exif Instance of Exif object with data
25-
* @throws \RuntimeException If the EXIF data could not be read
25+
* @return Exif Instance of Exif object with data
26+
* @throws PhpExifReaderException If the EXIF data could not be read
2627
*/
27-
public function getExifFromFile(string $file) : Exif;
28+
public function getExifFromFile(string $file): Exif;
2829
}

lib/PHPExif/Contracts/ReaderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ interface ReaderInterface
2323
* @param string $file
2424
* @return \PHPExif\Exif Instance of Exif object with data
2525
*/
26-
public function read(string $file) : Exif;
26+
public function read(string $file): Exif;
2727
}

0 commit comments

Comments
 (0)