Skip to content

Commit

Permalink
Code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
niektenhoopen committed May 4, 2022
1 parent 281ac7b commit dcac291
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"pluginName":"Photo EXIF","pluginDescription":"The plugin reads EXIF data from photos","pluginVersion":"1.0.0","pluginAuthorName":"NTH media","pluginVendorName":"nthmedia","pluginAuthorUrl":"https://nthmedia.nl","pluginAuthorGithub":"","codeComments":"","pluginComponents":["fieldtypes","services","twigextensions"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"coordinates","modelName":"","purchasableName":"","recordName":"","serviceName":"metadata","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
{"pluginName":"Photo EXIF","pluginDescription":"The plugin reads EXIF data from photos","pluginVersion":"2.0.0","pluginAuthorName":"NTH media","pluginVendorName":"nthmedia","pluginAuthorUrl":"https://nthmedia.nl","pluginAuthorGithub":"","codeComments":"","pluginComponents":["fieldtypes","services","twigextensions"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"coordinates","modelName":"","purchasableName":"","recordName":"","serviceName":"metadata","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
20 changes: 20 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use craft\ecs\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\EasyCodingStandard\ValueObject\Option;

return static function(ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [
__DIR__ . '/src',
__FILE__,
]);
$parameters->set(Option::SKIP, [
__DIR__ . '/tests/_craft/storage',
]);

$containerConfigurator->import(SetList::CRAFT_CMS_4);
};
19 changes: 19 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
parameters:
level: 5
paths:
- src
excludePaths:
scanFiles:
- vendor/twig/twig/src/Extension/CoreExtension.php
bootstrapFiles:
- vendor/yiisoft/yii2/Yii.php
- vendor/craftcms/cms/src/Craft.php
earlyTerminatingMethodCalls:
Craft:
- dd
yii\base\Application:
- end
yii\base\ErrorHandler:
- convertExceptionToError
ignoreErrors:
- '#Call to an undefined method yii\\base\\View:#'
46 changes: 22 additions & 24 deletions src/PhotoExif.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@

namespace nthmedia\photoexif;

use craft\base\Element;
use craft\elements\Asset;
use craft\events\RegisterElementTableAttributesEvent;
use nthmedia\photoexif\services\Metadata as MetadataService;
use nthmedia\photoexif\twigextensions\PhotoExifTwigExtension;
use nthmedia\photoexif\fields\Coordinates as CoordinatesField;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\elements\Asset;
use craft\events\PluginEvent;
use craft\services\Fields;

use craft\events\RegisterComponentTypesEvent;
use craft\services\Fields;
use craft\services\Plugins;
use nthmedia\photoexif\fields\Coordinates as CoordinatesField;
use nthmedia\photoexif\services\Metadata as MetadataService;
use nthmedia\photoexif\twigextensions\PhotoExifTwigExtension;

use yii\base\Event;

Expand Down Expand Up @@ -69,15 +67,15 @@ public function init()
Event::on(
Fields::class,
Fields::EVENT_REGISTER_FIELD_TYPES,
function (RegisterComponentTypesEvent $event) {
function(RegisterComponentTypesEvent $event) {
$event->types[] = CoordinatesField::class;
}
);

Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
function(PluginEvent $event) {
if ($event->plugin === $this) {
}
}
Expand All @@ -86,7 +84,7 @@ function (PluginEvent $event) {
Event::on(
Asset::class,
Asset::EVENT_BEFORE_VALIDATE,
function ($event) {
function($event) {
if ($event->sender->kind === 'image') {
$imagePath = $event->sender->tempFilePath ?? $event->sender->getImageTransformSourcePath();
if (exif_imagetype($imagePath)) {
Expand All @@ -100,7 +98,7 @@ function ($event) {
'GPSLatitude',
'GPSLatitudeRef',
'GPSLongitude',
'GPSLongitudeRef'
'GPSLongitudeRef',
];

foreach ($requiredFields as $requiredField) {
Expand All @@ -119,11 +117,11 @@ function ($event) {
$fields = $fieldLayout->getFields();

// Find the 'Coordinates' fields
$fields = array_filter($fields, function ($field) {
$fields = array_filter($fields, function($field) {
return get_class($field) === 'nthmedia\photoexif\fields\Coordinates';
});

array_walk($fields, function ($field) use ($event, $latitude, $longitude) {
array_walk($fields, function($field) use ($event, $latitude, $longitude) {
if ($event->sender->{$field['handle']} === null) {
$event->sender->{$field['handle']} = $latitude . "," . $longitude;
}
Expand All @@ -150,32 +148,32 @@ function ($event) {
/*
* Found in https://stackoverflow.com/a/2572991/9405801
*/
protected function getGps($exifCoord, $hemi) {

protected function getGps($exifCoord, $hemi)
{
$degrees = count($exifCoord) > 0 ? $this->gps2Num($exifCoord[0]) : 0;
$minutes = count($exifCoord) > 1 ? $this->gps2Num($exifCoord[1]) : 0;
$seconds = count($exifCoord) > 2 ? $this->gps2Num($exifCoord[2]) : 0;

$flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;

return $flip * ($degrees + $minutes / 60 + $seconds / 3600);

}

/*
* Found in https://stackoverflow.com/a/2572991/9405801
*/
protected function gps2Num($coordPart) {

protected function gps2Num($coordPart)
{
$parts = explode('/', $coordPart);

if (count($parts) <= 0)
if (count($parts) === 0) {
return 0;
}

if (count($parts) == 1)
if (count($parts) === 1) {
return $parts[0];
}

return floatval($parts[0]) / floatval($parts[1]);
return (float)$parts[0] / (float)$parts[1];
}

}
21 changes: 9 additions & 12 deletions src/fields/Coordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@

namespace nthmedia\photoexif\fields;

use craft\helpers\Html;
use nthmedia\photoexif\PhotoExif;
use nthmedia\photoexif\assetbundles\coordinatesfield\CoordinatesFieldAsset;

use Craft;
use craft\base\ElementInterface;

use craft\base\Field;
use craft\helpers\Db;
use yii\db\Schema;
use craft\helpers\Html;
use craft\helpers\Json;
use nthmedia\photoexif\PhotoExif;
use yii\db\Schema;

/**
* @author NTH media
Expand All @@ -39,7 +37,7 @@ class Coordinates extends Field implements \craft\base\PreviewableFieldInterface
/**
* @inheritdoc
*/
public static function hasContentColumn (): bool
public static function hasContentColumn(): bool
{
return true;
}
Expand All @@ -62,7 +60,7 @@ public function getElementValidationRules(): array
[
'match',
'pattern' => '/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/',
'message' => Craft::t('photo-exif', 'Coordinates are incorrectly formatted. (Correct: 12.34,56.78)')
'message' => Craft::t('photo-exif', 'Coordinates are incorrectly formatted. (Correct: 12.34,56.78)'),
], // https://stackoverflow.com/a/18690202/9405801
['default', 'value' => ''],
];
Expand Down Expand Up @@ -136,9 +134,9 @@ public function getTableAttributeHtml(mixed $value, ElementInterface $element):
if ($value) {
$value = preg_replace_callback(
'/^([-+]?([1-8]?\d(\.\d+))?|90(\.0+)?),\s*([-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+))?)$/',
function ($matches) {
$roundedCoordinates = number_format( (float) $matches[1], 4, '.', '');
$roundedCoordinates .= ', ' . number_format( (float) $matches[5], 4, '.', '');
function($matches) {
$roundedCoordinates = number_format((float) $matches[1], 4, '.', '');
$roundedCoordinates .= ', ' . number_format((float) $matches[5], 4, '.', '');

return Html::a(
$roundedCoordinates,
Expand All @@ -151,5 +149,4 @@ function ($matches) {
}
return '';
}

}
5 changes: 2 additions & 3 deletions src/services/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

namespace nthmedia\photoexif\services;

use nthmedia\photoexif\PhotoExif;

use Craft;
use craft\base\Component;

use nthmedia\photoexif\PhotoExif;

/**
* @author NTH media
* @package PhotoExif
Expand Down
2 changes: 0 additions & 2 deletions src/twigextensions/PhotoExifTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

use nthmedia\photoexif\PhotoExif;

use Craft;

/**
* @author NTH media
* @package PhotoExif
Expand Down

0 comments on commit dcac291

Please sign in to comment.