Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
#4960 #7110 - extracting logic for requiredStepIterations computati…
Browse files Browse the repository at this point in the history
…on into private method, renaming variables for clarity
  • Loading branch information
Ocramius committed Jan 12, 2015
1 parent c631b31 commit 6d5aeb2
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions library/Zend/Validator/DateStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,7 @@ public function isValid($value)
// iterations by starting at the lower bound of steps needed to reach
// the target

// Multiply the step interval by the lower bound of steps to reach the target
$minSteps = $this->getMinSteps($intervalParts, $diffParts);

// check for integer overflow and split $minimum interval if needed
$maximumInterval = max($intervalParts);
$stepIterationsRequired = 1;
// If we use PHP_INT_MAX DateInterval::__construct falls over with a bad format error
// before we reach the max on 64 bit machines
$maxInteger = min(pow(2, 31), PHP_INT_MAX);

if (($minSteps * $maximumInterval) > $maxInteger) {
$stepIterationsRequired = ceil(($minSteps * $maximumInterval) / $maxInteger);
$minSteps = floor($minSteps / $stepIterationsRequired);
}
list($minSteps, $requiredStepIterations) = $this->getMinStepAndRequiredStepIterations($intervalParts, $diffParts);

$multipliedParts = array();

Expand All @@ -366,7 +353,7 @@ public function isValid($value)

if ($baseDate < $valueDate) {
if ($minSteps > 0) {
for ($i = 0; $i < $stepIterationsRequired; $i += 1) {
for ($i = 0; $i < $requiredStepIterations; $i += 1) {
$baseDate->add($minimumInterval);
}
}
Expand All @@ -379,7 +366,7 @@ public function isValid($value)
}
} else {
if ($minSteps > 0) {
for ($i = 0; $i < $stepIterationsRequired; $i += 1) {
for ($i = 0; $i < $requiredStepIterations; $i += 1) {
$baseDate->sub($minimumInterval);
}
}
Expand All @@ -396,6 +383,31 @@ public function isValid($value)
return false;
}

/**
* @param int[] $intervalParts
* @param int[] $diffParts
*
* @return int[] (ordered tuple containing minimum steps and required step iterations
*/
private function getMinStepAndRequiredStepIterations(array $intervalParts, array $diffParts)
{
$minSteps = $this->getMinSteps($intervalParts, $diffParts);

// If we use PHP_INT_MAX DateInterval::__construct falls over with a bad format error
// before we reach the max on 64 bit machines
$maxInteger = min(pow(2, 31), PHP_INT_MAX);
// check for integer overflow and split $minimum interval if needed
$maximumInterval = max($intervalParts);
$requiredStepIterations = 1;

if (($minSteps * $maximumInterval) > $maxInteger) {
$requiredStepIterations = ceil(($minSteps * $maximumInterval) / $maxInteger);
$minSteps = floor($minSteps / $requiredStepIterations);
}

return array($minSteps, $requiredStepIterations);
}

/**
* Multiply the step interval by the lower bound of steps to reach the target
*
Expand Down

0 comments on commit 6d5aeb2

Please sign in to comment.