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

@@ -130,11 +130,12 @@ class Collection implements Countable, ArrayAccess, IteratorAggregate
{
if (!empty($elements)) {
foreach ($elements as $index => $element) {
if (!$useIndexes) {
$index = null;
if ($useIndexes) {
$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 = '')
{
$template = 'Method %s() cannot be called, because is disabled.';
$message = sprintf($template, $disabledMethod);
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);
}
}

View File

@@ -48,12 +48,10 @@ class Arrays
if (is_array($value)) {
$effect .= self::values2string($value, $arrayColumnKey, $separator);
} else {
if (empty($arrayColumnKey)) {
$effect .= $value;
} elseif ($key === $arrayColumnKey) {
$effect .= $array[$arrayColumnKey];
}
} elseif (empty($arrayColumnKey)) {
$effect .= $value;
} elseif ($key === $arrayColumnKey) {
$effect .= $array[$arrayColumnKey];
}
}
}
@@ -111,8 +109,8 @@ class Arrays
*/
if (is_array($row) && !empty($row)) {
foreach ($row as &$value) {
$value = html_entity_decode($value);
foreach ($row as $key => $value) {
$row[$key] = html_entity_decode($value);
}
$rows[] = implode($separator, $row);
@@ -363,7 +361,7 @@ class Arrays
* 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)
*/
if (self::isMultiDimensional($array) && empty($jsVariableName)) {
if (empty($jsVariableName) && $isMultiDimensional) {
$jsVariableName = 'autoGeneratedVariable';
}
@@ -384,13 +382,13 @@ class Arrays
if (is_array($value)) {
$variable = $index;
if (is_integer($index)) {
if (is_int($index)) {
$variable = 'value_' . $variable;
}
$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
* declaration of variable:
@@ -409,21 +407,19 @@ class Arrays
$effect .= "\n";
}
}
} else {
if ($preserveIndexes) {
if (!empty($jsVariableName)) {
$index = Miscellaneous::quoteValue($index);
$effect .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
}
} else {
$format = '%s';
if ($counter < $arrayCount) {
$format .= ', ';
}
$effect .= sprintf($format, $value);
} elseif ($preserveIndexes) {
if (!empty($jsVariableName)) {
$index = Miscellaneous::quoteValue($index);
$effect .= sprintf("\n%s[%s] = %s;", $jsVariableName, $index, $value);
}
} else {
$format = '%s';
if ($counter < $arrayCount) {
$format .= ', ';
}
$effect .= sprintf($format, $value);
}
}
@@ -474,7 +470,7 @@ class Arrays
{
if (is_string($item)) {
if ($last) {
$item = substr($item, 0, strlen($item) - 1);
$item = substr($item, 0, -1);
} else {
$item = substr($item, 1);
}
@@ -517,7 +513,7 @@ class Arrays
* No elements or the element does not exist?
* Nothing to do
*/
if (empty($array) || !in_array($item, $array)) {
if (empty($array) || !in_array($item, $array, true)) {
return false;
}
@@ -556,19 +552,18 @@ class Arrays
foreach ($array as $key => &$value) {
$remove = false;
$isArray = is_array($value);
if (is_array($value)) {
if ($isArray) {
self::removeElements($value, $needle, $before);
if (is_array($value) && empty($value)) {
if ($isArray && empty($value)) {
$remove = true;
}
} elseif ($value === $needle) {
break;
} else {
if ($value === $needle) {
break;
} else {
$remove = true;
}
$remove = true;
}
if ($remove) {
@@ -749,7 +744,7 @@ class Arrays
foreach ($exploded as $item) {
$exploded2 = explode($valuesKeysSeparator, $item);
if (2 == count($exploded2)) {
if (2 === count($exploded2)) {
$key = trim($exploded2[0]);
$value = trim($exploded2[1]);
@@ -781,19 +776,17 @@ class Arrays
if ($firstKey) {
$effect = $exists;
$firstKey = false;
} elseif ($explicit) {
$effect = $effect && $exists;
if (!$effect) {
break;
}
} else {
if ($explicit) {
$effect = $effect && $exists;
$effect = $effect || $exists;
if (!$effect) {
break;
}
} else {
$effect = $effect || $exists;
if ($effect) {
break;
}
if ($effect) {
break;
}
}
}
@@ -839,6 +832,7 @@ class Arrays
foreach ($array as $key => $value) {
$path = $key;
$stopRecursion = false;
$valueIsArray = is_array($value);
/*
* If the path of parent element is delivered,
@@ -871,7 +865,7 @@ class Arrays
* or
* - 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;
continue;
}
@@ -879,7 +873,7 @@ class Arrays
/*
* Let's iterate through the next level, using recursive
*/
if (is_array($value)) {
if ($valueIsArray) {
$recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
$paths += $recursivePaths;
}
@@ -1100,7 +1094,8 @@ class Arrays
$recursiveValues = self::getAllValuesOfKey($value, $key);
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)) {
$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?
* 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);
}
@@ -1371,58 +1366,55 @@ class Arrays
if ($array2HasKey && is_array($array2[$key])) {
$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
* 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) {
$effect[] = $difference;
}
} else {
/*
* The key exists in 2nd array?
*/
if ($array2HasKey) {
/*
* The value it's an array (it's a nested array)?
*/
if (is_array($value)) {
$diff = [];
* The key exists in 2nd array?
*/
} elseif ($array2HasKey) {
/*
* The value it's an array (it's a nested array)?
*/
if (is_array($value)) {
$diff = [];
if (is_array($array2[$key])) {
/*
* Let's verify the nested array
*/
$diff = self::arrayDiffRecursive($value, $array2[$key], $valuesOnly);
}
if (empty($diff)) {
continue;
}
$effect[$key] = $diff;
} else {
if (is_array($array2[$key])) {
/*
* Value is different than in 2nd array?
* OKay, I've got difference
* Let's verify the nested array
*/
if ($value != $array2[$key]) {
$effect[$key] = $value;
}
$diff = self::arrayDiffRecursive($value, $array2[$key], $valuesOnly);
}
} else {
if (empty($diff)) {
continue;
}
$effect[$key] = $diff;
/*
* Value is different than in 2nd array?
* OKay, I've got difference
*/
} elseif ($value !== $array2[$key]) {
$effect[$key] = $value;
}
} else {
/*
* OKay, I've got difference
*/
$effect[$key] = $value;
}
}
@@ -1595,17 +1587,17 @@ class Arrays
$noPrevious = !$next && self::isFirstElement($array, $element);
/*
* No elements?
* OR
* Given element does not exist in given array?
* Previous neighbour should be returned and given element is first?
* OR
* Next neighbour should be returned and given element is last?
* 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
*/
if (empty($array) || !in_array($element, $array) || $noNext || $noPrevious) {
if ($noPrevious || $noNext || empty($array) || !in_array($element, $array, true)) {
return null;
}

View File

@@ -140,7 +140,12 @@ class Date
$dateStart = 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;
if (DatePeriod::LAST_YEAR === $period) {
@@ -167,7 +172,7 @@ class Date
return null;
}
$dateStart->setTime(0, 0, 0);
$dateStart->setTime(0, 0);
$dateEnd->setTime(23, 59, 59);
return new DatePeriod($dateStart, $dateEnd);
@@ -217,7 +222,8 @@ class Date
return $dateTime
->setTime($hour, $minute, $second)
->format($format);
->format($format)
;
}
/**

View File

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

View File

@@ -733,7 +733,7 @@ class MimeTypes
*/
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);
return $data[$mimeType];
@@ -806,7 +806,7 @@ class MimeTypes
*/
public static function isImage($mimeType)
{
if (in_array($mimeType, self::$mimeTypes)) {
if (in_array($mimeType, self::$mimeTypes, true)) {
return (bool)preg_match('|^image/.+$|', $mimeType);
}

View File

@@ -10,7 +10,6 @@ namespace Meritoo\Common\Utilities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\Query\Parameter;
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
* key-value or an array of arrays where second element of sub-array is the
* 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
*
* Example of the $criteria argument:
@@ -107,7 +106,7 @@ class QueryBuilderUtility
* '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?
@@ -121,7 +120,7 @@ class QueryBuilderUtility
* No alias provided?
* Let's use root alias
*/
if (empty($alias)) {
if (null === $alias || '' === $alias) {
$alias = self::getRootAlias($queryBuilder);
}
@@ -129,7 +128,7 @@ class QueryBuilderUtility
$compareOperator = '=';
if (is_array($value) && !empty($value)) {
if (2 == count($value)) {
if (2 === count($value)) {
$compareOperator = $value[1];
}
@@ -158,7 +157,6 @@ class QueryBuilderUtility
* @param array|ArrayCollection $entities The entities to delete
* @param bool $flushDeleted (optional) If is set to true, flushes the deleted objects (default
* behaviour). Otherwise - not.
* @throws OptimisticLockException
* @return bool
*/
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?
* Nothing to do
*/
if (10 !== strlen($taxId) || !is_numeric($taxId)) {
if (!is_numeric($taxId) || 10 !== strlen($taxId)) {
return false;
}
@@ -125,14 +125,11 @@ class Regex
}
/*
* Last number it's not a remainder from dividing per 11?
* Nothing to do
* Last number it's a remainder from dividing per 11?
* 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) {
$matchesCount = preg_match($filterExpression, $value);
$remove = 0 == $matchesCount;
$remove = 0 === $matchesCount;
} else {
if (is_string($value)) {
$value = sprintf('\'%s\'', $value);
@@ -306,11 +303,9 @@ class Regex
if ($mustAllMatch) {
$effect = $effect && $matched;
} else {
if ($matched) {
$effect = $matched;
break;
}
} elseif ($matched) {
$effect = $matched;
break;
}
}
@@ -501,7 +496,7 @@ class Regex
public static function startsWith($string, $beginning)
{
if (!empty($string) && !empty($beginning)) {
if (1 == strlen($beginning) && !self::isLetterOrDigit($beginning)) {
if (1 === strlen($beginning) && !self::isLetterOrDigit($beginning)) {
$beginning = '\\' . $beginning;
}
@@ -522,7 +517,7 @@ class Regex
*/
public static function endsWith($string, $ending)
{
if (1 == strlen($ending) && !self::isLetterOrDigit($ending)) {
if (1 === strlen($ending) && !self::isLetterOrDigit($ending)) {
$ending = '\\' . $ending;
}
@@ -607,7 +602,7 @@ class Regex
*/
public static function contains($haystack, $needle)
{
if (1 == strlen($needle) && !self::isLetterOrDigit($needle)) {
if (1 === strlen($needle) && !self::isLetterOrDigit($needle)) {
$needle = '\\' . $needle;
}
@@ -694,14 +689,14 @@ class Regex
*/
public static function isValidNip($nip)
{
$nip = preg_replace('/[^0-9]/', '', $nip);
$nip = preg_replace('/[\D]/', '', $nip);
$invalidNips = [
'1234567890',
'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;
}
@@ -723,9 +718,9 @@ class Regex
}
$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
*/
if (0 == $matchCount) {
if (0 === $matchCount) {
return '';
}

View File

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