Skip to content

Commit

Permalink
Code-Pflege via phpStan, phpMD und phpCS (#36)
Browse files Browse the repository at this point in the history
* bug fixes via phpstan
  • Loading branch information
akrys authored Apr 27, 2024
1 parent 2d1fbe3 commit a814f66
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 159 deletions.
6 changes: 5 additions & 1 deletion boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

use FriendsOfRedaxo\addon\MediapoolExif\MediapoolExif;

rex_fragment::addDirectory(realpath(__DIR__));
$dir = realpath(__DIR__);
if ($dir !== false) {
rex_fragment::addDirectory($dir);
}


$class = MediapoolExif::class;
rex_extension::register('MEDIA_ADDED', [$class, 'processUploadedMedia'], rex_extension::LATE);
Expand Down
16 changes: 8 additions & 8 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
->ensureColumn(new rex_sql_column('exif', 'longtext', true))
->ensure();

rex_metainfo_add_field('Beschreibung', 'med_description', '1', '', '2', '', '', '', '');
rex_metainfo_add_field('Schlagworte', 'med_keywords', '2', '', '1', '', '', '', '');
rex_metainfo_add_field('Copyright', 'med_copyright', '3', '', '1', '', '', '', '');
rex_metainfo_add_field('Autor', 'med_author', '4', '', '1', '', '', '', '');
rex_metainfo_add_field('Ausrichtung', 'med_orientation', '5', '', '1', '', '', '', '');
rex_metainfo_add_field('Erstellungsdatum', 'med_createdate', '6', '', '1', '', '', '', '');
rex_metainfo_add_field('GPS Breitengrad', 'med_gps_lat', '7', '', '1', '', '', '', '');
rex_metainfo_add_field('GPS Längengrad', 'med_gps_long', '8', '', '1', '', '', '', '');
rex_metainfo_add_field('Beschreibung', 'med_description', 1, '', 2, '', '', '', '');
rex_metainfo_add_field('Schlagworte', 'med_keywords', 2, '', 1, '', '', '', '');
rex_metainfo_add_field('Copyright', 'med_copyright', 3, '', 1, '', '', '', '');
rex_metainfo_add_field('Autor', 'med_author', 4, '', 1, '', '', '', '');
rex_metainfo_add_field('Ausrichtung', 'med_orientation', 5, '', 1, '', '', '', '');
rex_metainfo_add_field('Erstellungsdatum', 'med_createdate', 6, '', 1, '', '', '', '');
rex_metainfo_add_field('GPS Breitengrad', 'med_gps_lat', 7, '', 1, '', '', '', '');
rex_metainfo_add_field('GPS Längengrad', 'med_gps_long', 8, '', 1, '', '', '', '');

22 changes: 14 additions & 8 deletions lib/Cli/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Description of Renew
*
* @author akrys
* @codeCoverageIgnore
*/
class Read extends rex_console_command
{
Expand All @@ -42,33 +43,38 @@ protected function configure(): void
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = $this->getStyle($input, $output);
$io->title('Read EXIF data');
$command = $this->getStyle($input, $output);
$command->title('Read EXIF data');

$updateAll = $input->getOption('all');
$updateSilent = $input->getOption('silent');

$mode = MediaFetchMode::NULL_ONLY;
if ($updateAll) {
if ($updateSilent || $io->confirm('You are going to update all files. Are you sure?', false)) {
if ($updateSilent || $command->confirm('You are going to update all files. Are you sure?', false)) {
$mode = MediaFetchMode::ALL;
}
}
$files = Exif::getMediaToRead($mode);

$numEntries = count($files);
$io->writeln($numEntries.' entries to read');
$command->writeln($numEntries.' entries to read');

$counter = 0;
foreach ($files as $file) {
if (!isset($file['filename'])) {
continue;
}

$counter++;
$io->writeln('Process file '.$counter.' of '.$numEntries.': '.$file['filename']);
$command->writeln('Process file '.$counter.' of '.$numEntries.': '.$file['filename']);

MediapoolExif::readExifFromFile($file['filename']);
MediapoolExif::readExifFromFile((string) $file['filename']);
}

$io->success('done');
$command->success('done');
return self::SUCCESS;
}
}
3 changes: 2 additions & 1 deletion lib/Enum/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*
* @author akrys
*/
enum Format :string
enum Format: string
{
case RAW = 'numeric';
case READABLE = 'readable';
case UNDEFINED = '';
}
3 changes: 1 addition & 2 deletions lib/Enum/MediaFetchMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/

namespace FriendsOfRedaxo\addon\MediapoolExif\Enum;

/**
Expand All @@ -19,7 +18,7 @@
*
* @author akrys
*/
enum MediaFetchMode :int
enum MediaFetchMode: int
{
case NULL_ONLY = 1000;
case ALL = 1001;
Expand Down
54 changes: 54 additions & 0 deletions lib/Exception/InvalidClassException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
namespace FriendsOfRedaxo\addon\MediapoolExif\Exception;

use Exception;
use Throwable;

/**
* Datei für ...
*
* @version 1.0 / 2024-03-31
* @author akrys
*/

/**
* Description of InvalidClassException
*
* @author akrys
*/
class InvalidClassException extends Exception
{

/**
* Konstruktor
* @param string $class
* @param string $message
* @param int $code
* @param Throwable $previous
*/
public function __construct(
private string $class,
string $message = "",
int $code = 0,
Throwable $previous = null
) {
if ($message === '') {
$message = 'Invalid class: '.$class;
}
parent::__construct($message, $code, $previous);
}

/**
* Formatname
* @return string
*/
public function getClass(): string
{
return $this->class;
}
}
24 changes: 13 additions & 11 deletions lib/Exception/InvalidFormatExcption.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,34 @@
*/
class InvalidFormatExcption extends Exception
{
/**
* @var Format
*/
public Format $format;

/**
* Konstruktor
* @param string $format
* @param ?Format $format
* @param string $message
* @param int $code
* @param Throwable $previous
*/
public function __construct(Format $format, string $message = "", int $code = 0, Throwable $previous = NULL)
{
$this->format = $format;
public function __construct(
private ?Format $format,
string $message = "",
int $code = 0,
Throwable $previous = null
) {
if ($this->format === null) {
$this->format = Format::UNDEFINED;
}
if ($message === '') {
$message = 'Invalid Format: '.$format->value;
$message = 'Invalid Format: '.$this->format->value;
}
parent::__construct($message, $code, $previous);
}

/**
* Formatname
* @return Format
* @return Format|null
*/
public function getFormat(): Format
public function getFormat(): ?Format
{
return $this->format;
}
Expand Down
13 changes: 6 additions & 7 deletions lib/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
*/
class NotFoundException extends Exception
{
/**
* @var string
*/
public string $index;

/**
* Konstruktor
Expand All @@ -30,9 +26,12 @@ class NotFoundException extends Exception
* @param int $code
* @param Throwable $previous
*/
public function __construct(string $index, string $message = "", int $code = 0, Throwable $previous = NULL)
{
$this->index = $index;
public function __construct(
private string $index,
string $message = "",
int $code = 0,
Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
}

Expand Down
7 changes: 4 additions & 3 deletions lib/Exif.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
*/
class Exif
{

/**
* Alternative Datenerhebung, analog zu rex_media::get()
*
* @param \FriendsOfRedaxo\addon\MediapoolExif\rex_media $media
* @param rex_media $media
* @return ExifData
*/
public static function get(rex_media $media, ReturnMode $mode = ReturnMode::THROW_EXCEPTION): ExifData
Expand All @@ -34,8 +35,8 @@ public static function get(rex_media $media, ReturnMode $mode = ReturnMode::THRO

/**
*
* @param type $mode
* @return array
* @param MediaFetchMode $mode
* @return list<array<int|string, scalar|null>>|array<int|string, scalar|null>
*/
public static function getMediaToRead(MediaFetchMode $mode = MediaFetchMode::NULL_ONLY): array
{
Expand Down
38 changes: 12 additions & 26 deletions lib/ExifData.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,13 @@
*/
class ExifData
{
/**
* Media-Objekt
*
* @var rex_media
*/
private rex_media $media;

/**
* Exif-Daten-Array
*
* @var array
* @var array<string, mixed>
*/
private array $exif;

/**
* Modus
* @var ReturnMode
*/
private ReturnMode $mode;

/**
* Konstruktor
*
Expand All @@ -63,26 +50,26 @@ class ExifData
* <li>[] (MODE_RETURN_EMPTY_ARRAY)</li>
* </ol>
*
* @param \FriendsOfRedaxo\addon\MediapoolExif\rex_media $media
* @param int $mode
* @param rex_media $media
* @param ReturnMode $mode
*/
public function __construct(rex_media $media, ReturnMode $mode = null)
{
$this->media = $media;
public function __construct(
private rex_media $media,
private ?ReturnMode $mode = null
) {
$this->exif = [];

$exifRaw = $this->media->getValue('exif');
if ($exifRaw !== null) {
$this->exif = json_decode($exifRaw, true);
$this->exif = json_decode((string) $exifRaw, true);
if (!$this->exif) {
$this->exif = [];
}
}

if ($mode === null) {
$mode = ReturnMode::THROW_EXCEPTION;
if ($this->mode === null) {
$this->mode = ReturnMode::THROW_EXCEPTION;
}
$this->mode = $mode;
}

/**
Expand All @@ -108,9 +95,8 @@ public function get(string $index = null): mixed

/**
* Formatierungsalgorithmus anstoßen
* @param string $type
* @param Format $format
* @param string $className
* @param Format $format
* @return mixed
*/
public function format(string $className, Format $format = null): mixed
Expand All @@ -132,7 +118,7 @@ public function format(string $className, Format $format = null): mixed
*
* Welche Rückgabe hätten's gern?
*
* @param string $exception
* @param Exception $exception
* @return mixed
* @throws NotFoundException
*/
Expand Down
Loading

0 comments on commit a814f66

Please sign in to comment.