Skip to content

Commit

Permalink
TASK: Code style and type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
ComiR authored and daniellienert committed Nov 7, 2018
1 parent 7807b0d commit 5d41675
Show file tree
Hide file tree
Showing 21 changed files with 400 additions and 271 deletions.
96 changes: 92 additions & 4 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,94 @@
preset: psr2

finder:
path:
- "Neos.MetaData.Extractor/Classes"
- "Neos.MetaData.Extractor/Tests"
enabled:
- alpha_ordered_imports
- array_indentation
- binary_operator_spaces
- combine_consecutive_issets
- combine_consecutive_unsets
- comment_to_phpdoc
- compact_nullable_typehint
- concat_with_spaces
- fully_qualified_strict_types
- function_typehint_space
- hash_to_slash_comment
- include
- is_null
- linebreak_after_opening_tag
- lowercase_cast
- magic_constant_casing
- method_chaining_indentation
- method_separation
- modernize_types_casting
- multiline_comment_opening_closing
- native_function_casing
- native_function_invocation
- newline_before_semicolons_chained
- no_alias_functions
- no_alternative_syntax
- no_blank_lines_after_class_opening
- no_blank_lines_after_phpdoc
- no_blank_lines_after_return
- no_blank_lines_after_throw
- no_blank_lines_before_namespace
- no_blank_lines_between_imports
- no_empty_comment
- no_empty_phpdoc
- no_empty_statement
- no_extra_block_blank_lines
- no_homoglyph_names
- no_leading_import_slash
- no_leading_namespace_whitespace
- no_multiline_whitespace_around_double_arrow
- no_null_property_initialization
- no_php4_constructor
- no_short_bool_cast
- no_singleline_whitespace_before_semicolons
- no_spaces_inside_offset
- no_spaces_outside_offset
- no_superfluous_elseif
- no_trailing_comma_in_singleline_array
- no_unneeded_curly_braces
- no_unneeded_final_method
- no_unused_imports
- no_useless_else
- no_useless_return
- no_whitespace_before_comma_in_array
- no_whitespace_in_blank_line
- non_printable_character
- normalize_index_brace
- object_operator_without_whitespace
- phpdoc_add_missing_param_annotation
- phpdoc_annotation_without_dot
- phpdoc_indent
- phpdoc_no_access
- phpdoc_no_package
- phpdoc_no_useless_inheritdoc
- phpdoc_return_self_reference
- phpdoc_scalar
- phpdoc_single_line_var_spacing
- phpdoc_trim
- phpdoc_type_to_var
- phpdoc_types
- post_increment
- print_to_echo
- property_separation
- self_accessor
- semicolon_after_instruction
- short_array_syntax
- short_list_syntax
- short_scalar_cast
- simplified_null_return
- single_quote
- space_after_semicolon
- standardize_increment
- standardize_not_equals
- strict_comparison
- ternary_operator_spaces
- ternary_to_null_coalescing
- trailing_comma_in_multiline_array
- trim_array_spaces
- unalign_double_arrow
- unalign_equals
- unary_operator_spaces
- whitespace_after_comma_in_array
10 changes: 6 additions & 4 deletions Classes/Command/MetaDataCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* This file is part of the Neos.MetaData.Extractor package.
*/

use Neos\Flow\Persistence\Doctrine\PersistenceManager;
use Neos\MetaData\Extractor\Domain\ExtractionManager;
use Neos\MetaData\Extractor\Exception\ExtractorException;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Persistence\Doctrine\PersistenceManager;
use Neos\Media\Domain\Model\Asset;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\MetaData\Extractor\Domain\ExtractionManager;
use Neos\MetaData\Extractor\Exception\ExtractorException;

/**
* @Flow\Scope("singleton")
Expand All @@ -38,6 +38,8 @@ class MetaDataCommandController extends CommandController

/**
* Extracts MetaData from Assets
*
* @return void
*/
public function extractCommand()
{
Expand All @@ -55,7 +57,7 @@ public function extractCommand()

$this->output->progressAdvance(1);

if($iterator->key() % 100 === 0) {
if ($iterator->key() % 100 === 0) {
$this->persistenceManager->persistAll();
}
}
Expand Down
13 changes: 6 additions & 7 deletions Classes/Converter/CoordinatesConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ class CoordinatesConverter
* Converts coordinates in DMS (degrees, minutes, seconds) and the cardinal direction reference (E,W,N,S) into
* DD (decimal degrees) notation.
*
* @param array<float> $dmsArray Coordinates in DMS (degrees, minutes, seconds)
* @param float[] $dmsArray Coordinates in DMS (degrees, minutes, seconds)
* @param string $cardinalDirectionReference cardinal direction reference (E,W,N,S)
*
* @return float Coordinates in DD (decimal degrees)
*/
public static function convertDmsToDd($dmsArray, $cardinalDirectionReference)
public static function convertDmsToDd(array $dmsArray, string $cardinalDirectionReference) : float
{
$degrees = isset($dmsArray[0]) ? $dmsArray[0] : 0.0;
$minutes = isset($dmsArray[1]) ? $dmsArray[1] : 0.0;
$seconds = isset($dmsArray[2]) ? $dmsArray[2] : 0.0;
$degrees = $dmsArray[0] ?? 0.0;
$minutes = $dmsArray[1] ?? 0.0;
$seconds = $dmsArray[2] ?? 0.0;

$flip = ($cardinalDirectionReference === 'W' || $cardinalDirectionReference === 'S') ? -1 : 1;

return round($flip * ($degrees + $minutes / 60 + $seconds / 3600), 6);
return \round($flip * ($degrees + $minutes / 60 + $seconds / 3600), 6);
}
}
28 changes: 12 additions & 16 deletions Classes/Converter/DateConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,30 @@ class DateConverter
* Combines the EXIF GPSTimeStamp and GPSDateStamp into a DateTime object
*
* @param string $gpsDateStamp
* @param array $gpsTimeStamp
*
* @param string[] $gpsTimeStamp
* @return \DateTime
*/
public static function convertGpsDateAndTime($gpsDateStamp, $gpsTimeStamp)
public static function convertGpsDateAndTime(string $gpsDateStamp, array $gpsTimeStamp) : \DateTime
{
return \DateTime::createFromFormat('Y:m:d H:i:s', $gpsDateStamp . ' ' . (int)$gpsTimeStamp[0] . ':' . (int)$gpsTimeStamp[1] . ':' . (int)$gpsTimeStamp[2]);
return \DateTime::createFromFormat(
'Y:m:d H:i:s',
$gpsDateStamp . ' ' . (int)$gpsTimeStamp[0] . ':' . (int)$gpsTimeStamp[1] . ':' . (int)$gpsTimeStamp[2]
);
}

/**
* Combines ISO 8601 like date and time string into a DateTime Object
*
* @param $dateString
* @param $timeString
* @return bool|\DateTime
* @param string $dateString
* @param string|null $timeString
* @return \DateTime|bool
*/
public static function convertIso8601DateAndTimeString($dateString, $timeString)
public static function convertIso8601DateAndTimeString(string $dateString, string $timeString = null)
{
if (empty($dateString)) {
return false;
}

if (empty($timeString)) {
$timeString = '000000+0000';
} else {
if(!strpos($timeString, '+')) {
$timeString .= '+0000';
}
} elseif (\strpos($timeString, '+') === false && \strpos($timeString, '-') === false) {
$timeString .= '+0000';
}

return \DateTime::createFromFormat('YmdHisO', $dateString . $timeString);
Expand Down
17 changes: 8 additions & 9 deletions Classes/Converter/NumberConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ class NumberConverter
* Converts a rational string like EXIF / (S)RATIONAL into a float number.
*
* @param string $rationalString
*
* @return float
*/
public static function convertRationalToFloat($rationalString)
public static function convertRationalToFloat(string $rationalString) : float
{
if (preg_match('#^(-?\d+)\/(\d+)$#', $rationalString, $matches)) {
if (\preg_match('#^(-?\d+)\/(\d+)$#', $rationalString, $matches)) {
$divisor = (float)$matches[2];
if ($divisor !== 0.0) {
return (int)$matches[1] / $divisor;
Expand All @@ -38,16 +37,16 @@ public static function convertRationalToFloat($rationalString)
}

/**
* Converts a version in the format like 0x02020000
* to 2.2.0.0
* Converts a version in the format like 0x02020000 to 2.2.0.0
*
* @param string $binaryVersion
* @return string
*/
public static function convertBinaryToVersion($binaryVersion)
public static function convertBinaryToVersion(string $binaryVersion) : string
{
$versionParts = str_split((string)bin2hex($binaryVersion), 2);
$versionParts = array_map('intval', $versionParts);
return implode('.', $versionParts);
$versionParts = \str_split(\bin2hex($binaryVersion), 2);
$versionParts = \array_map('\intval', $versionParts);

return \implode('.', $versionParts);
}
}
100 changes: 53 additions & 47 deletions Classes/Domain/ExtractionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,47 +49,10 @@ class ExtractionManager
*/
protected $metaDataManager;

/**
* @param Asset $asset
*
* @return MetaDataCollection
* @throws ExtractorException
* @throws UnknownObjectException
*/
public function extractMetaData(Asset $asset)
{
if ($asset instanceof ImageVariant) {
$asset = $asset->getOriginalAsset();
}

$flowResource = $asset->getResource();
if ($flowResource === null) {
throw new ExtractorException('Resource of Asset "' . $asset->getTitle() . '"" not found.', 1484060541);
}

$metaDataCollection = new MetaDataCollection();
$this->buildAssetMetaData($asset, $metaDataCollection);

$suitableAdapterClasses = $this->findSuitableExtractorAdaptersForResource($flowResource);
foreach ($suitableAdapterClasses as $suitableAdapterClass) {
/** @var ExtractorInterface $suitableAdapter */
$suitableAdapter = $this->objectManager->get($suitableAdapterClass);
try {
$suitableAdapter->extractMetaData($flowResource, $metaDataCollection);
} catch (ExtractorException $exception) {
//Extractor is theoretically suitable but failed to extract meta data
continue;
}
}

$this->metaDataManager->updateMetaDataForAsset($asset, $metaDataCollection);

return $metaDataCollection;
}

/**
* @param Asset $asset
* @param MetaDataCollection $metaDataCollection
* @return void
*/
protected function buildAssetMetaData(Asset $asset, MetaDataCollection $metaDataCollection)
{
Expand Down Expand Up @@ -119,18 +82,61 @@ protected function buildAssetMetaData(Asset $asset, MetaDataCollection $metaData

/**
* @param FlowResource $flowResource
*
* @return array
* @return string[] Class names
*/
protected function findSuitableExtractorAdaptersForResource(FlowResource $flowResource)
protected function findSuitableExtractorAdaptersForResource(FlowResource $flowResource) : array
{
$extractorAdapters = $this->reflectionService->getAllImplementationClassNamesForInterface(ExtractorInterface::class);

$suitableAdapterClasses = array_filter($extractorAdapters, function ($extractorAdapterClass) use ($flowResource) {
/** @var ExtractorInterface $extractorAdapterClass */
return $extractorAdapterClass::isSuitableFor($flowResource);
});
/** @noinspection PhpUnhandledExceptionInspection */
$extractorAdapters = $this->reflectionService->getAllImplementationClassNamesForInterface(
ExtractorInterface::class
);

$suitableAdapterClasses = \array_filter(
$extractorAdapters,
function ($extractorAdapterClass) use ($flowResource) {
/** @var ExtractorInterface $extractorAdapterClass */
return $extractorAdapterClass::isSuitableFor($flowResource);
}
);

return $suitableAdapterClasses;
}

/**
* @param Asset $asset
* @return MetaDataCollection
* @throws ExtractorException
* @throws UnknownObjectException
*/
public function extractMetaData(Asset $asset) : MetaDataCollection
{
if ($asset instanceof ImageVariant) {
$asset = $asset->getOriginalAsset();
}

$flowResource = $asset->getResource();
if ($flowResource === null) {
throw new ExtractorException('Resource of Asset "' . $asset->getTitle() . '"" not found.', 1484060541);
}

$metaDataCollection = new MetaDataCollection();
$this->buildAssetMetaData($asset, $metaDataCollection);

$suitableAdapterClasses = $this->findSuitableExtractorAdaptersForResource($flowResource);
foreach ($suitableAdapterClasses as $suitableAdapterClass) {
/** @var ExtractorInterface $suitableAdapter */
/** @noinspection PhpUnhandledExceptionInspection */
$suitableAdapter = $this->objectManager->get($suitableAdapterClass);
try {
$suitableAdapter->extractMetaData($flowResource, $metaDataCollection);
} catch (ExtractorException $exception) {
//Extractor is theoretically suitable but failed to extract meta data
continue;
}
}

$this->metaDataManager->updateMetaDataForAsset($asset, $metaDataCollection);

return $metaDataCollection;
}
}
4 changes: 2 additions & 2 deletions Classes/Domain/Extractor/AbstractExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ abstract class AbstractExtractor implements ExtractorInterface
/**
* The media types this adapter can handle
*
* @var array
* @var string[]
*/
protected static $compatibleMediaTypes = [];

/**
* @inheritDoc
*/
public static function isSuitableFor(PersistentResource $resource)
public static function isSuitableFor(PersistentResource $resource) : bool
{
$mediaType = $resource->getMediaType();
foreach (static::$compatibleMediaTypes as $compatibleMediaType) {
Expand Down
Loading

0 comments on commit 5d41675

Please sign in to comment.