Minor refactoring

This commit is contained in:
Meritoo
2019-02-21 21:27:50 +01:00
parent 8c3c85608a
commit 651c4f2259
11 changed files with 126 additions and 130 deletions

View File

@@ -10,6 +10,10 @@
} }
], ],
"require": { "require": {
"ext-dom": "*",
"ext-fileinfo": "*",
"ext-json": "*",
"ext-simplexml": "*",
"php": ">=5.6", "php": ">=5.6",
"ext-intl": "*", "ext-intl": "*",
"ext-pcre": "*", "ext-pcre": "*",

View File

@@ -130,11 +130,12 @@ class Collection implements Countable, ArrayAccess, IteratorAggregate
{ {
if (!empty($elements)) { if (!empty($elements)) {
foreach ($elements as $index => $element) { foreach ($elements as $index => $element) {
if (!$useIndexes) { if ($useIndexes) {
$index = null; $this->add($element, $index);
continue;
} }
$this->add($element, $index); $this->add($element);
} }
} }

View File

@@ -28,13 +28,13 @@ class DisabledMethodException extends Exception
public static function create($disabledMethod, $alternativeMethod = '') public static function create($disabledMethod, $alternativeMethod = '')
{ {
$template = 'Method %s() cannot be called, because is disabled.'; $template = 'Method %s() cannot be called, because is disabled.';
$message = sprintf($template, $disabledMethod);
if (!empty($alternativeMethod)) { if (!empty($alternativeMethod)) {
$template .= ' Use %s() instead.'; $template = '%s Use %s() instead.';
$message = sprintf($template, $message, $alternativeMethod);
} }
$message = sprintf($template, $disabledMethod, $alternativeMethod);
return new static($message); return new static($message);
} }
} }

View File

@@ -48,15 +48,13 @@ class Arrays
if (is_array($value)) { if (is_array($value)) {
$effect .= self::values2string($value, $arrayColumnKey, $separator); $effect .= self::values2string($value, $arrayColumnKey, $separator);
} else { } elseif (empty($arrayColumnKey)) {
if (empty($arrayColumnKey)) {
$effect .= $value; $effect .= $value;
} elseif ($key === $arrayColumnKey) { } elseif ($key === $arrayColumnKey) {
$effect .= $array[$arrayColumnKey]; $effect .= $array[$arrayColumnKey];
} }
} }
} }
}
return $effect; return $effect;
} }
@@ -111,8 +109,8 @@ class Arrays
*/ */
if (is_array($row) && !empty($row)) { if (is_array($row) && !empty($row)) {
foreach ($row as &$value) { foreach ($row as $key => $value) {
$value = html_entity_decode($value); $row[$key] = html_entity_decode($value);
} }
$rows[] = implode($separator, $row); $rows[] = implode($separator, $row);
@@ -363,7 +361,7 @@ class Arrays
* Name of the variable was not provided and it's a multi dimensional array? * Name of the variable was not provided and it's a multi dimensional array?
* Let's create the name, because variable is required for later usage (related to multi dimensional array) * Let's create the name, because variable is required for later usage (related to multi dimensional array)
*/ */
if (self::isMultiDimensional($array) && empty($jsVariableName)) { if (empty($jsVariableName) && $isMultiDimensional) {
$jsVariableName = 'autoGeneratedVariable'; $jsVariableName = 'autoGeneratedVariable';
} }
@@ -384,13 +382,13 @@ class Arrays
if (is_array($value)) { if (is_array($value)) {
$variable = $index; $variable = $index;
if (is_integer($index)) { if (is_int($index)) {
$variable = 'value_' . $variable; $variable = 'value_' . $variable;
} }
$value = self::array2JavaScript($value, $variable, $preserveIndexes); $value = self::array2JavaScript($value, $variable, $preserveIndexes);
if (!empty($value)) { if (null !== $value && '' !== $value) {
/* /*
* Add an empty line for the 1st iteration only. Required to avoid missing empty line after * Add an empty line for the 1st iteration only. Required to avoid missing empty line after
* declaration of variable: * declaration of variable:
@@ -409,8 +407,7 @@ class Arrays
$effect .= "\n"; $effect .= "\n";
} }
} }
} else { } elseif ($preserveIndexes) {
if ($preserveIndexes) {
if (!empty($jsVariableName)) { if (!empty($jsVariableName)) {
$index = Miscellaneous::quoteValue($index); $index = Miscellaneous::quoteValue($index);
$effect .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value); $effect .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
@@ -425,7 +422,6 @@ class Arrays
$effect .= sprintf($format, $value); $effect .= sprintf($format, $value);
} }
} }
}
if (!$preserveIndexes && !$isMultiDimensional) { if (!$preserveIndexes && !$isMultiDimensional) {
$effect .= ');'; $effect .= ');';
@@ -474,7 +470,7 @@ class Arrays
{ {
if (is_string($item)) { if (is_string($item)) {
if ($last) { if ($last) {
$item = substr($item, 0, strlen($item) - 1); $item = substr($item, 0, -1);
} else { } else {
$item = substr($item, 1); $item = substr($item, 1);
} }
@@ -517,7 +513,7 @@ class Arrays
* No elements or the element does not exist? * No elements or the element does not exist?
* Nothing to do * Nothing to do
*/ */
if (empty($array) || !in_array($item, $array)) { if (empty($array) || !in_array($item, $array, true)) {
return false; return false;
} }
@@ -556,20 +552,19 @@ class Arrays
foreach ($array as $key => &$value) { foreach ($array as $key => &$value) {
$remove = false; $remove = false;
$isArray = is_array($value);
if (is_array($value)) { if ($isArray) {
self::removeElements($value, $needle, $before); self::removeElements($value, $needle, $before);
if (is_array($value) && empty($value)) { if ($isArray && empty($value)) {
$remove = true; $remove = true;
} }
} else { } elseif ($value === $needle) {
if ($value === $needle) {
break; break;
} else { } else {
$remove = true; $remove = true;
} }
}
if ($remove) { if ($remove) {
unset($array[$key]); unset($array[$key]);
@@ -749,7 +744,7 @@ class Arrays
foreach ($exploded as $item) { foreach ($exploded as $item) {
$exploded2 = explode($valuesKeysSeparator, $item); $exploded2 = explode($valuesKeysSeparator, $item);
if (2 == count($exploded2)) { if (2 === count($exploded2)) {
$key = trim($exploded2[0]); $key = trim($exploded2[0]);
$value = trim($exploded2[1]); $value = trim($exploded2[1]);
@@ -781,8 +776,7 @@ class Arrays
if ($firstKey) { if ($firstKey) {
$effect = $exists; $effect = $exists;
$firstKey = false; $firstKey = false;
} else { } elseif ($explicit) {
if ($explicit) {
$effect = $effect && $exists; $effect = $effect && $exists;
if (!$effect) { if (!$effect) {
@@ -797,7 +791,6 @@ class Arrays
} }
} }
} }
}
return $effect; return $effect;
} }
@@ -839,6 +832,7 @@ class Arrays
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
$path = $key; $path = $key;
$stopRecursion = false; $stopRecursion = false;
$valueIsArray = is_array($value);
/* /*
* If the path of parent element is delivered, * If the path of parent element is delivered,
@@ -871,7 +865,7 @@ class Arrays
* or * or
* - the process is stopped, recursive is not used * - the process is stopped, recursive is not used
*/ */
if (!is_array($value) || (is_array($value) && empty($value)) || $stopRecursion) { if (!$valueIsArray || ($valueIsArray && empty($value)) || $stopRecursion) {
$paths[$path] = $value; $paths[$path] = $value;
continue; continue;
} }
@@ -879,7 +873,7 @@ class Arrays
/* /*
* Let's iterate through the next level, using recursive * Let's iterate through the next level, using recursive
*/ */
if (is_array($value)) { if ($valueIsArray) {
$recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy); $recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
$paths += $recursivePaths; $paths += $recursivePaths;
} }
@@ -1100,7 +1094,8 @@ class Arrays
$recursiveValues = self::getAllValuesOfKey($value, $key); $recursiveValues = self::getAllValuesOfKey($value, $key);
if (!empty($recursiveValues)) { if (!empty($recursiveValues)) {
$values = array_merge($values, $recursiveValues); $merged = array_merge($values, $recursiveValues);
$values = $merged;
} }
} }
} }
@@ -1282,7 +1277,7 @@ class Arrays
} }
if (Regex::endsWith($element, $separator)) { if (Regex::endsWith($element, $separator)) {
$element = substr($element, 0, strlen($element) - 1); $element = substr($element, 0, -1);
} }
} }
@@ -1354,7 +1349,7 @@ class Arrays
* Values should be compared only and both arrays are one-dimensional? * Values should be compared only and both arrays are one-dimensional?
* Let's find difference by using simple function * Let's find difference by using simple function
*/ */
if ($valuesOnly && 1 == self::getDimensionsCount($array1) && 1 == self::getDimensionsCount($array2)) { if ($valuesOnly && 1 === self::getDimensionsCount($array1) && 1 === self::getDimensionsCount($array2)) {
return array_diff($array1, $array2); return array_diff($array1, $array2);
} }
@@ -1371,25 +1366,24 @@ class Arrays
if ($array2HasKey && is_array($array2[$key])) { if ($array2HasKey && is_array($array2[$key])) {
$difference = self::arrayDiffRecursive($value, $array2[$key], $valuesOnly); $difference = self::arrayDiffRecursive($value, $array2[$key], $valuesOnly);
} }
} else { } elseif (!$array2HasKey || ($array2HasKey && $value !== $array2[$key])) {
/* /*
* 2nd array hasn't key from 1st array? * We are here, because:
* a) 2nd array hasn't key from 1st array
* OR * OR
* Key exists in both, 1st and 2nd array, but values are different? * b) key exists in both, 1st and 2nd array, but values are different
*/ */
if (!$array2HasKey || ($array2HasKey && $value != $array2[$key])) {
$difference = $value; $difference = $value;
} }
}
if (null !== $difference) { if (null !== $difference) {
$effect[] = $difference; $effect[] = $difference;
} }
} else {
/* /*
* The key exists in 2nd array? * The key exists in 2nd array?
*/ */
if ($array2HasKey) { } elseif ($array2HasKey) {
/* /*
* The value it's an array (it's a nested array)? * The value it's an array (it's a nested array)?
*/ */
@@ -1408,15 +1402,14 @@ class Arrays
} }
$effect[$key] = $diff; $effect[$key] = $diff;
} else {
/* /*
* Value is different than in 2nd array? * Value is different than in 2nd array?
* OKay, I've got difference * OKay, I've got difference
*/ */
if ($value != $array2[$key]) { } elseif ($value !== $array2[$key]) {
$effect[$key] = $value; $effect[$key] = $value;
} }
}
} else { } else {
/* /*
* OKay, I've got difference * OKay, I've got difference
@@ -1424,7 +1417,6 @@ class Arrays
$effect[$key] = $value; $effect[$key] = $value;
} }
} }
}
return $effect; return $effect;
} }
@@ -1595,17 +1587,17 @@ class Arrays
$noPrevious = !$next && self::isFirstElement($array, $element); $noPrevious = !$next && self::isFirstElement($array, $element);
/* /*
* No elements? * Previous neighbour should be returned and given element is first?
* OR
* Given element does not exist in given array?
* OR * OR
* Next neighbour should be returned and given element is last? * Next neighbour should be returned and given element is last?
* OR * OR
* Previous neighbour should be returned and given element is first? * No elements?
* OR
* Given element does not exist in given array?
* *
* Nothing to do * Nothing to do
*/ */
if (empty($array) || !in_array($element, $array) || $noNext || $noPrevious) { if ($noPrevious || $noNext || empty($array) || !in_array($element, $array, true)) {
return null; return null;
} }

View File

@@ -140,7 +140,12 @@ class Date
$dateStart = new DateTime(); $dateStart = new DateTime();
$dateEnd = new DateTime(); $dateEnd = new DateTime();
if (DatePeriod::LAST_YEAR === $period || DatePeriod::NEXT_YEAR === $period) { $yearPeriod = [
DatePeriod::LAST_YEAR,
DatePeriod::NEXT_YEAR,
];
if (in_array($period, $yearPeriod, true)) {
$yearDifference = 1; $yearDifference = 1;
if (DatePeriod::LAST_YEAR === $period) { if (DatePeriod::LAST_YEAR === $period) {
@@ -167,7 +172,7 @@ class Date
return null; return null;
} }
$dateStart->setTime(0, 0, 0); $dateStart->setTime(0, 0);
$dateEnd->setTime(23, 59, 59); $dateEnd->setTime(23, 59, 59);
return new DatePeriod($dateStart, $dateEnd); return new DatePeriod($dateStart, $dateEnd);
@@ -217,7 +222,8 @@ class Date
return $dateTime return $dateTime
->setTime($hour, $minute, $second) ->setTime($hour, $minute, $second)
->format($format); ->format($format)
;
} }
/** /**

View File

@@ -52,7 +52,7 @@ class Locale
LC_MESSAGES, LC_MESSAGES,
]; ];
if (empty($languageCode) || !in_array($category, $availableCategories)) { if (empty($languageCode) || !in_array($category, $availableCategories, true)) {
return false; return false;
} }

View File

@@ -733,7 +733,7 @@ class MimeTypes
*/ */
public static function getExtension($mimeType) public static function getExtension($mimeType)
{ {
if (is_string($mimeType) && in_array($mimeType, self::$mimeTypes)) { if (is_string($mimeType) && in_array($mimeType, self::$mimeTypes, true)) {
$data = Arrays::setKeysAsValues(self::$mimeTypes, false); $data = Arrays::setKeysAsValues(self::$mimeTypes, false);
return $data[$mimeType]; return $data[$mimeType];
@@ -806,7 +806,7 @@ class MimeTypes
*/ */
public static function isImage($mimeType) public static function isImage($mimeType)
{ {
if (in_array($mimeType, self::$mimeTypes)) { if (in_array($mimeType, self::$mimeTypes, true)) {
return (bool)preg_match('|^image/.+$|', $mimeType); return (bool)preg_match('|^image/.+$|', $mimeType);
} }

View File

@@ -10,7 +10,6 @@ namespace Meritoo\Common\Utilities;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\Query\Parameter; use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
@@ -91,7 +90,7 @@ class QueryBuilderUtility
* @param array $criteria (optional) The criteria used in WHERE clause. It may simple array with pairs * @param array $criteria (optional) The criteria used in WHERE clause. It may simple array with pairs
* key-value or an array of arrays where second element of sub-array is the * key-value or an array of arrays where second element of sub-array is the
* comparison operator. Example below. * comparison operator. Example below.
* @param string $alias (optional) Alias used in the query * @param string|null $alias (optional) Alias used in the query
* @return QueryBuilder * @return QueryBuilder
* *
* Example of the $criteria argument: * Example of the $criteria argument:
@@ -107,7 +106,7 @@ class QueryBuilderUtility
* 'position' => 5, * 'position' => 5,
* ] * ]
*/ */
public static function setCriteria(QueryBuilder $queryBuilder, array $criteria = [], $alias = '') public static function setCriteria(QueryBuilder $queryBuilder, array $criteria = [], $alias = null)
{ {
/* /*
* No criteria used in WHERE clause? * No criteria used in WHERE clause?
@@ -121,7 +120,7 @@ class QueryBuilderUtility
* No alias provided? * No alias provided?
* Let's use root alias * Let's use root alias
*/ */
if (empty($alias)) { if (null === $alias || '' === $alias) {
$alias = self::getRootAlias($queryBuilder); $alias = self::getRootAlias($queryBuilder);
} }
@@ -129,7 +128,7 @@ class QueryBuilderUtility
$compareOperator = '='; $compareOperator = '=';
if (is_array($value) && !empty($value)) { if (is_array($value) && !empty($value)) {
if (2 == count($value)) { if (2 === count($value)) {
$compareOperator = $value[1]; $compareOperator = $value[1];
} }
@@ -158,7 +157,6 @@ class QueryBuilderUtility
* @param array|ArrayCollection $entities The entities to delete * @param array|ArrayCollection $entities The entities to delete
* @param bool $flushDeleted (optional) If is set to true, flushes the deleted objects (default * @param bool $flushDeleted (optional) If is set to true, flushes the deleted objects (default
* behaviour). Otherwise - not. * behaviour). Otherwise - not.
* @throws OptimisticLockException
* @return bool * @return bool
*/ */
public static function deleteEntities(EntityManager $entityManager, $entities, $flushDeleted = true) public static function deleteEntities(EntityManager $entityManager, $entities, $flushDeleted = true)

View File

@@ -102,7 +102,7 @@ class Regex
* Tax ID is not 10 characters length OR is not numeric? * Tax ID is not 10 characters length OR is not numeric?
* Nothing to do * Nothing to do
*/ */
if (10 !== strlen($taxId) || !is_numeric($taxId)) { if (!is_numeric($taxId) || 10 !== strlen($taxId)) {
return false; return false;
} }
@@ -125,14 +125,11 @@ class Regex
} }
/* /*
* Last number it's not a remainder from dividing per 11? * Last number it's a remainder from dividing per 11?
* Nothing to do * Tax ID is valid
*/ */
if ($sum % 11 == $taxId[9]) {
return true;
}
return false; return $sum % 11 === (int)$taxId[9];
} }
/** /**
@@ -248,7 +245,7 @@ class Regex
if ($itsRegularExpression) { if ($itsRegularExpression) {
$matchesCount = preg_match($filterExpression, $value); $matchesCount = preg_match($filterExpression, $value);
$remove = 0 == $matchesCount; $remove = 0 === $matchesCount;
} else { } else {
if (is_string($value)) { if (is_string($value)) {
$value = sprintf('\'%s\'', $value); $value = sprintf('\'%s\'', $value);
@@ -306,13 +303,11 @@ class Regex
if ($mustAllMatch) { if ($mustAllMatch) {
$effect = $effect && $matched; $effect = $effect && $matched;
} else { } elseif ($matched) {
if ($matched) {
$effect = $matched; $effect = $matched;
break; break;
} }
} }
}
return $effect; return $effect;
} }
@@ -501,7 +496,7 @@ class Regex
public static function startsWith($string, $beginning) public static function startsWith($string, $beginning)
{ {
if (!empty($string) && !empty($beginning)) { if (!empty($string) && !empty($beginning)) {
if (1 == strlen($beginning) && !self::isLetterOrDigit($beginning)) { if (1 === strlen($beginning) && !self::isLetterOrDigit($beginning)) {
$beginning = '\\' . $beginning; $beginning = '\\' . $beginning;
} }
@@ -522,7 +517,7 @@ class Regex
*/ */
public static function endsWith($string, $ending) public static function endsWith($string, $ending)
{ {
if (1 == strlen($ending) && !self::isLetterOrDigit($ending)) { if (1 === strlen($ending) && !self::isLetterOrDigit($ending)) {
$ending = '\\' . $ending; $ending = '\\' . $ending;
} }
@@ -607,7 +602,7 @@ class Regex
*/ */
public static function contains($haystack, $needle) public static function contains($haystack, $needle)
{ {
if (1 == strlen($needle) && !self::isLetterOrDigit($needle)) { if (1 === strlen($needle) && !self::isLetterOrDigit($needle)) {
$needle = '\\' . $needle; $needle = '\\' . $needle;
} }
@@ -694,14 +689,14 @@ class Regex
*/ */
public static function isValidNip($nip) public static function isValidNip($nip)
{ {
$nip = preg_replace('/[^0-9]/', '', $nip); $nip = preg_replace('/[\D]/', '', $nip);
$invalidNips = [ $invalidNips = [
'1234567890', '1234567890',
'0000000000', '0000000000',
]; ];
if (!preg_match('/^[0-9]{10}$/', $nip) || in_array($nip, $invalidNips)) { if (!preg_match('/^[\d]{10}$/', $nip) || in_array($nip, $invalidNips, true)) {
return false; return false;
} }
@@ -723,9 +718,9 @@ class Regex
} }
$modulo = $sum % 11; $modulo = $sum % 11;
$numberControl = (10 == $modulo) ? 0 : $modulo; $numberControl = (10 === $modulo) ? 0 : $modulo;
return $numberControl == $nip[9]; return $numberControl === (int)$nip[9];
} }
/** /**

View File

@@ -93,7 +93,7 @@ class Uri
/* /*
* Oops, cannot match protocol * Oops, cannot match protocol
*/ */
if (0 == $matchCount) { if (0 === $matchCount) {
return ''; return '';
} }

View File

@@ -40,7 +40,7 @@ class Xml
$query = $path->query('/*/*'); $query = $path->query('/*/*');
$nodesCount = $query->length; $nodesCount = $query->length;
if (0 == $nodesCount) { if (0 === $nodesCount) {
return $element1; return $element1;
} }