Skip to content

Commit

Permalink
Remove unnecessary exception and null checks (#63)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
qwerty287 and ildyria authored Apr 6, 2023
1 parent 8460af5 commit bfc651d
Show file tree
Hide file tree
Showing 30 changed files with 409 additions and 864 deletions.
16 changes: 8 additions & 8 deletions lib/PHPExif/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(array $options = array())
* @param \PHPExif\Contracts\MapperInterface $mapper
* @return \PHPExif\Contracts\AdapterInterface
*/
public function setMapper(MapperInterface $mapper) : AdapterInterface
public function setMapper(MapperInterface $mapper): AdapterInterface
{
$this->mapper = $mapper;

Expand All @@ -55,12 +55,12 @@ public function setMapper(MapperInterface $mapper) : AdapterInterface
*
* @return \PHPExif\Contracts\MapperInterface
*/
public function getMapper() : MapperInterface
public function getMapper(): MapperInterface
{
if (null === $this->mapper) {
// lazy load one
/** @var MapperInterface */
$mapper = new $this->mapperClass;
$mapper = new $this->mapperClass();

$this->setMapper($mapper);
}
Expand All @@ -74,7 +74,7 @@ public function getMapper() : MapperInterface
* @param \PHPExif\Contracts\HydratorInterface $hydrator
* @return \PHPExif\Contracts\AdapterInterface
*/
public function setHydrator(HydratorInterface $hydrator) : AdapterInterface
public function setHydrator(HydratorInterface $hydrator): AdapterInterface
{
$this->hydrator = $hydrator;

Expand All @@ -86,12 +86,12 @@ public function setHydrator(HydratorInterface $hydrator) : AdapterInterface
*
* @return \PHPExif\Contracts\HydratorInterface
*/
public function getHydrator() : HydratorInterface
public function getHydrator(): HydratorInterface
{
if (null === $this->hydrator) {
// lazy load one
/** @var HydratorInterface */
$hydrator = new $this->hydratorClass;
$hydrator = new $this->hydratorClass();

$this->setHydrator($hydrator);
}
Expand All @@ -105,7 +105,7 @@ public function getHydrator() : HydratorInterface
* @param array $options
* @return \PHPExif\Contracts\AdapterInterface
*/
public function setOptions(array $options) : AdapterInterface
public function setOptions(array $options): AdapterInterface
{
$hydrator = $this->getHydrator();
$hydrator->hydrate($this, $options);
Expand All @@ -122,7 +122,7 @@ public function setOptions(array $options) : AdapterInterface
*/
// @codeCoverageIgnoreStart
// this is fine because we use it directly in our tests for Exiftool and Native
public function convertToUTF8(array|string $data) : array|string
public function convertToUTF8(array|string $data): array|string
{
if (is_array($data)) {
/** @var array|string|null $v */
Expand Down
38 changes: 23 additions & 15 deletions lib/PHPExif/Adapter/Exiftool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use PHPExif\Exif;
use InvalidArgumentException;
use PHPExif\Mapper\Exiftool as MapperExiftool;
use RuntimeException;
use PHPExif\Reader\PhpExifReaderException;
use Safe\Exceptions\ExecException;

use Safe\Exceptions\JsonException;

use function Safe\exec;
use function Safe\json_decode;
use function Safe\stream_get_contents;
Expand All @@ -23,7 +25,7 @@
*/
class Exiftool extends AbstractAdapter
{
const TOOL_NAME = 'exiftool';
public const TOOL_NAME = 'exiftool';

/**
* Path to the exiftool binary
Expand All @@ -40,7 +42,7 @@ class Exiftool extends AbstractAdapter
* @return \PHPExif\Adapter\Exiftool Current instance
* @throws \InvalidArgumentException When path is invalid
*/
public function setToolPath(string $path) : Exiftool
public function setToolPath(string $path): Exiftool
{
if (!file_exists($path)) {
throw new InvalidArgumentException(
Expand All @@ -59,7 +61,7 @@ public function setToolPath(string $path) : Exiftool
/**
* @param boolean $numeric
*/
public function setNumeric(bool $numeric) : void
public function setNumeric(bool $numeric): void
{
$this->numeric = $numeric;
}
Expand All @@ -68,7 +70,7 @@ public function setNumeric(bool $numeric) : void
* @see http://www.sno.phy.queensu.ca/~phil/exiftool/faq.html#Q10
* @param array $encodings encoding parameters in an array eg. ["exif" => "UTF-8"]
*/
public function setEncoding(array $encodings) : void
public function setEncoding(array $encodings): void
{
$possible_keys = array("exif", "iptc", "id3", "photoshop", "quicktime",);
$possible_values = array("UTF8", "cp65001", "UTF-8", "Thai", "cp874", "Latin", "cp1252",
Expand All @@ -90,7 +92,7 @@ public function setEncoding(array $encodings) : void
*
* @return string
*/
public function getToolPath() : string
public function getToolPath(): string
{
if ($this->toolPath === '') {
try {
Expand All @@ -109,10 +111,10 @@ public function getToolPath() : string
* Reads & parses the EXIF data from given file
*
* @param string $file
* @return \PHPExif\Exif Instance of Exif object with data
* @throws \RuntimeException If the EXIF data could not be read
* @return Exif Instance of Exif object with data
* @throws PhpExifReaderException If the EXIF data could not be read
*/
public function getExifFromFile(string $file) : Exif
public function getExifFromFile(string $file): Exif
{
$encoding = '';
if (count($this->encoding) > 0) {
Expand All @@ -136,14 +138,20 @@ public function getExifFromFile(string $file) : Exif
);

/**
* @var string
* @var string $result
*/
$result = $this->convertToUTF8($result);

$data = json_decode($result, true);
try {
$data = json_decode($result, true);
} catch (JsonException $e) {
// @codeCoverageIgnoreStart
$data = false;
// @codeCoverageIgnoreStart
}
if (!is_array($data)) {
// @codeCoverageIgnoreStart
throw new RuntimeException(
throw new PhpExifReaderException(
'Could not decode exiftool output'
);
// @codeCoverageIgnoreEnd
Expand Down Expand Up @@ -171,9 +179,9 @@ public function getExifFromFile(string $file) : Exif
*
* @param string $command
* @return string|false
* @throws RuntimeException If the command can't be executed
* @throws PhpExifReaderException If the command can't be executed
*/
protected function getCliOutput(string $command) : string|false
protected function getCliOutput(string $command): string|false
{
$descriptorspec = array(
0 => array('pipe', 'r'),
Expand All @@ -184,7 +192,7 @@ protected function getCliOutput(string $command) : string|false
$process = proc_open($command, $descriptorspec, $pipes);

if (!is_resource($process)) {
throw new RuntimeException(
throw new PhpExifReaderException(
'Could not open a resource to the exiftool binary'
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/PHPExif/Adapter/FFprobe.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use InvalidArgumentException;
use FFMpeg;
use PHPExif\Mapper\FFprobe as MapperFFprobe;
use RuntimeException;
use PHPExif\Reader\PhpExifReaderException;
use Safe\Exceptions\ExecException;

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

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

$ffprobe = FFMpeg\FFProbe::create(array(
Expand Down
6 changes: 3 additions & 3 deletions lib/PHPExif/Adapter/ImageMagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class ImageMagick extends AbstractAdapter
{
const TOOL_NAME = 'imagick';
public const TOOL_NAME = 'imagick';

protected string $mapperClass = MapperImageMagick::class;

Expand Down Expand Up @@ -49,7 +49,7 @@ class ImageMagick extends AbstractAdapter
* @param string $file
* @return \PHPExif\Exif Instance of Exif object with data
*/
public function getExifFromFile(string $file) : Exif
public function getExifFromFile(string $file): Exif
{
/* Create the object */
$im = new Imagick($file);
Expand Down Expand Up @@ -98,7 +98,7 @@ public function getExifFromFile(string $file) : Exif
* @param string $profile Raw IPTC data
* @return array
*/
public function getIptcData(string $profile) : array
public function getIptcData(string $profile): array
{
$arrData = [];
try {
Expand Down
42 changes: 21 additions & 21 deletions lib/PHPExif/Adapter/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
*/
class Native extends AbstractAdapter
{
const INCLUDE_THUMBNAIL = true;
const NO_THUMBNAIL = false;
public const INCLUDE_THUMBNAIL = true;
public const NO_THUMBNAIL = false;

const SECTIONS_AS_ARRAYS = true;
const SECTIONS_FLAT = false;
public const SECTIONS_AS_ARRAYS = true;
public const SECTIONS_FLAT = false;

const SECTION_FILE = 'FILE';
const SECTION_COMPUTED = 'COMPUTED';
const SECTION_IFD0 = 'IFD0';
const SECTION_THUMBNAIL = 'THUMBNAIL';
const SECTION_COMMENT = 'COMMENT';
const SECTION_EXIF = 'EXIF';
const SECTION_ALL = 'ANY_TAG';
const SECTION_IPTC = 'IPTC';
public const SECTION_FILE = 'FILE';
public const SECTION_COMPUTED = 'COMPUTED';
public const SECTION_IFD0 = 'IFD0';
public const SECTION_THUMBNAIL = 'THUMBNAIL';
public const SECTION_COMMENT = 'COMMENT';
public const SECTION_EXIF = 'EXIF';
public const SECTION_ALL = 'ANY_TAG';
public const SECTION_IPTC = 'IPTC';

/**
* List of EXIF sections
Expand Down Expand Up @@ -79,7 +79,7 @@ class Native extends AbstractAdapter
*
* @return array
*/
public function getRequiredSections() : array
public function getRequiredSections(): array
{
return $this->requiredSections;
}
Expand All @@ -90,7 +90,7 @@ public function getRequiredSections() : array
* @param array $sections List of EXIF sections
* @return \PHPExif\Adapter\Native Current instance for chaining
*/
public function setRequiredSections(array $sections) : Native
public function setRequiredSections(array $sections): Native
{
$this->requiredSections = $sections;

Expand All @@ -103,7 +103,7 @@ public function setRequiredSections(array $sections) : Native
* @param string $section
* @return \PHPExif\Adapter\Native Current instance for chaining
*/
public function addRequiredSection(string $section) : Native
public function addRequiredSection(string $section): Native
{
if (!in_array($section, $this->requiredSections, true)) {
array_push($this->requiredSections, $section);
Expand All @@ -118,7 +118,7 @@ public function addRequiredSection(string $section) : Native
* @param boolean $value
* @return \PHPExif\Adapter\Native Current instance for chaining
*/
public function setIncludeThumbnail(bool $value) : Native
public function setIncludeThumbnail(bool $value): Native
{
$this->includeThumbnail = $value;

Expand All @@ -130,7 +130,7 @@ public function setIncludeThumbnail(bool $value) : Native
*
* @return boolean
*/
public function getIncludeThumbnail() : bool
public function getIncludeThumbnail(): bool
{
return $this->includeThumbnail;
}
Expand All @@ -141,7 +141,7 @@ public function getIncludeThumbnail() : bool
* @param boolean $value
* @return \PHPExif\Adapter\Native Current instance for chaining
*/
public function setSectionsAsArrays(bool $value) : Native
public function setSectionsAsArrays(bool $value): Native
{
$this->sectionsAsArrays = $value;

Expand All @@ -153,7 +153,7 @@ public function setSectionsAsArrays(bool $value) : Native
*
* @return boolean
*/
public function getSectionsAsArrays() : bool
public function getSectionsAsArrays(): bool
{
return $this->sectionsAsArrays;
}
Expand All @@ -164,7 +164,7 @@ public function getSectionsAsArrays() : bool
* @param string $file
* @return \PHPExif\Exif Instance of Exif object with data
*/
public function getExifFromFile(string $file) : Exif
public function getExifFromFile(string $file): Exif
{
$mimeType = mime_content_type($file);

Expand Down Expand Up @@ -245,7 +245,7 @@ public function getExifFromFile(string $file) : Exif
* @param string $file The file to read the IPTC data from
* @return array
*/
public function getIptcData(string $file) : array
public function getIptcData(string $file): array
{
getimagesize($file, $info);
$arrData = array();
Expand Down
21 changes: 0 additions & 21 deletions lib/PHPExif/Adapter/NoAdapterException.php

This file was deleted.

7 changes: 4 additions & 3 deletions lib/PHPExif/Contracts/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace PHPExif\Contracts;

use PHPExif\Exif;
use PHPExif\Reader\PhpExifReaderException;

/**
* PHP Exif Reader Adapter
Expand All @@ -21,8 +22,8 @@ interface AdapterInterface
* Reads & parses the EXIF data from given file
*
* @param string $file
* @return \PHPExif\Exif Instance of Exif object with data
* @throws \RuntimeException If the EXIF data could not be read
* @return Exif Instance of Exif object with data
* @throws PhpExifReaderException If the EXIF data could not be read
*/
public function getExifFromFile(string $file) : Exif;
public function getExifFromFile(string $file): Exif;
}
2 changes: 1 addition & 1 deletion lib/PHPExif/Contracts/ReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ interface ReaderInterface
* @param string $file
* @return \PHPExif\Exif Instance of Exif object with data
*/
public function read(string $file) : Exif;
public function read(string $file): Exif;
}
Loading

0 comments on commit bfc651d

Please sign in to comment.