8000 [Form] Clean unused code by alamirault · Pull Request #52660 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Clean unused code #52660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form] Clean unused code
  • Loading branch information
alamirault authored and fabpot committed Nov 23, 2023
commit 1b4a246e8db2bcdc8d730424e59e87f945d71a08
Original file line number Diff line number Diff line change
Expand Up @@ -183,35 +183,21 @@ protected function castParsedValue(int|float $value): int|float
*/
private function round(int|float $number): int|float
{
if (null !== $this->scale && null !== $this->roundingMode) {
if (null !== $this->scale) {
// shift number to maintain the correct scale during rounding
$roundingCoef = 10 ** $this->scale;
// string representation to avoid rounding errors, similar to bcmul()
$number = (string) ($number * $roundingCoef);

switch ($this->roundingMode) {
case \NumberFormatter::ROUND_CEILING:
$number = ceil($number);
break;
case \NumberFormatter::ROUND_FLOOR:
$number = floor($number);
break;
case \NumberFormatter::ROUND_UP:
$number = $number > 0 ? ceil($number) : floor($number);
break;
case \NumberFormatter::ROUND_DOWN:
$number = $number > 0 ? floor($number) : ceil($number);
break;
case \NumberFormatter::ROUND_HALFEVEN:
$number = round($number, 0, \PHP_ROUND_HALF_EVEN);
break;
case \NumberFormatter::ROUND_HALFUP:
$number = round($number, 0, \PHP_ROUND_HALF_UP);
break;
case \NumberFormatter::ROUND_HALFDOWN:
$number = round($number, 0, \PHP_ROUND_HALF_DOWN);
break;
}
$number = match ($this->roundingMode) {
\NumberFormatter::ROUND_CEILING => ceil($number),
\NumberFormatter::ROUND_FLOOR => floor($number),
\NumberFormatter::ROUND_UP => $number > 0 ? ceil($number) : floor($number),
\NumberFormatter::ROUND_DOWN => $number > 0 ? floor($number) : ceil($number),
\NumberFormatter::ROUND_HALFEVEN => round($number, 0, \PHP_ROUND_HALF_EVEN),
\NumberFormatter::ROUND_HALFUP => round($number, 0, \PHP_ROUND_HALF_UP),
\NumberFormatter::ROUND_HALFDOWN => round($number, 0, \PHP_ROUND_HALF_DOWN),
};

$number = 1 === $roundingCoef ? (int) $number : $number / $roundingCoef;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ protected function getNumberFormatter(): \NumberFormatter

$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale);

if (null !== $this->roundingMode) {
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);
}
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);

return $formatter;
}
Expand All @@ -192,43 +190,27 @@ protected function getNumberFormatter(): \NumberFormatter
*/
private function round(int|float $number): int|float
{
if (null !== $this->scale && null !== $this->roundingMode) {
// shift number to maintain the correct scale during rounding
$roundingCoef = 10 ** $this->scale;
// shift number to maintain the correct scale during rounding
$roundingCoef = 10 ** $this->scale;

if (self::FRACTIONAL == $this->type) {
$roundingCoef *= 100;
}
if (self::FRACTIONAL === $this->type) {
$roundingCoef *= 100;
}

// string representation to avoid rounding errors, similar to bcmul()
$number = (string) ($number * $roundingCoef);

switch ($this->roundingMode) {
case \NumberFormatter::ROUND_CEILING:
$number = ceil($number);
break;
case \NumberFormatter::ROUND_FLOOR:
$number = floor($number);
break;
case \NumberFormatter::ROUND_UP:
$number = $number > 0 ? ceil($number) : floor($number);
break;
case \NumberFormatter::ROUND_DOWN:
$number = $number > 0 ? floor($number) : ceil($number);
break;
case \Numbe 8000 rFormatter::ROUND_HALFEVEN:
$number = round($number, 0, \PHP_ROUND_HALF_EVEN);
break;
case \NumberFormatter::ROUND_HALFUP:
$number = round($number, 0, \PHP_ROUND_HALF_UP);
break;
case \NumberFormatter::ROUND_HALFDOWN:
$number = round($number, 0, \PHP_ROUND_HALF_DOWN);
break;
}
// string representation to avoid rounding errors, similar to bcmul()
$number = (string) ($number * $roundingCoef);

$number = 1 === $roundingCoef ? (int) $number : $number / $roundingCoef;
}
$number = match ($this->roundingMode) {
\NumberFormatter::ROUND_CEILING => ceil($number),
\NumberFormatter::ROUND_FLOOR => floor($number),
\NumberFormatter::ROUND_UP => $number > 0 ? ceil($number) : floor($number),
\NumberFormatter::ROUND_DOWN => $number > 0 ? floor($number) : ceil($number),
\NumberFormatter::ROUND_HALFEVEN => round($number, 0, \PHP_ROUND_HALF_EVEN),
\NumberFormatter::ROUND_HALFUP => round($number, 0, \PHP_ROUND_HALF_UP),
\NumberFormatter::ROUND_HALFDOWN => round($number, 0, \PHP_ROUND_HALF_DOWN),
};

$number = 1 === $roundingCoef ? (int) $number : $number / $roundingCoef;

return $number;
}
Expand Down
7 changes: 0 additions & 7 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\Form\Exception\OutOfBoundsException;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Util\FormUtil;
use Symfony\Component\Form\Util\InheritDataAwareIterator;
Expand Down Expand Up @@ -727,12 +726,6 @@ public function add(FormInterface|string $child, string $type = null, array $opt
}

if (!$child instanceof FormInterface) {
if (!\is_string($child) && !\is_int($child)) {
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormInterface');
}

$child = (string) $child;

// Never initialize child forms automatically
$options['auto_initialize'] = false;

Expand Down
5 changes: 0 additions & 5 deletions src/Symfony/Component/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\Type\TextType;

/**
Expand Down Expand Up @@ -60,10 +59,6 @@ public function add(FormBuilderInterface|string $child, string $type = null, arr
return $this;
}

if (!\is_string($child) && !\is_int($child)) {
throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormBuilderInterface');
}

// Add to "children" to maintain order
$this->children[$child] = null;
$this->unresolvedChildren[$child] = [$type, $options];
Expand Down
0