fix: SA: Fix "...variable always exists and is not falsy...." rule.

SA stands for Static Analysis.
This commit is contained in:
Pol Dellaiera 2021-11-16 15:23:56 +01:00
parent 11651fdb2a
commit c94fb2efbf
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
2 changed files with 28 additions and 50 deletions

View File

@ -70,11 +70,6 @@ parameters:
count: 1 count: 1
path: src/Bundle/ChillMainBundle/Form/ChoiceLoader/PostalCodeChoiceLoader.php path: src/Bundle/ChillMainBundle/Form/ChoiceLoader/PostalCodeChoiceLoader.php
-
message: "#^Variable \\$value in empty\\(\\) always exists and is not falsy\\.$#"
count: 1
path: src/Bundle/ChillMainBundle/Form/Type/DataTransformer/DateIntervalTransformer.php
- -
message: "#^Variable \\$message on left side of \\?\\? always exists and is not nullable\\.$#" message: "#^Variable \\$message on left side of \\?\\? always exists and is not nullable\\.$#"
count: 1 count: 1

View File

@ -1,38 +1,15 @@
<?php <?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop> declare(strict_types=1);
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Form\Type\DataTransformer; namespace Chill\MainBundle\Form\Type\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class DateIntervalTransformer implements DataTransformerInterface class DateIntervalTransformer implements DataTransformerInterface
{ {
/**
*
* @param \DateInterval $value
* @throws UnexpectedTypeException
*/
public function transform($value) public function transform($value)
{ {
if (empty($value)) { if (empty($value)) {
@ -50,31 +27,36 @@ class DateIntervalTransformer implements DataTransformerInterface
'n' => $value->d / 7, 'n' => $value->d / 7,
'unit' => 'W' 'unit' => 'W'
]; ];
} else { }
return [ return [
'n' => $value->d, 'n' => $value->d,
'unit' => 'D' 'unit' => 'D'
]; ];
} }
} elseif ($value->m > 0) {
if ($value->m > 0) {
return [ return [
'n' => $value->m, 'n' => $value->m,
'unit' => 'M' 'unit' => 'M'
]; ];
} elseif ($value->y > 0) { }
if ($value->y > 0) {
return [ return [
'n' => $value->y, 'n' => $value->y,
'unit' => 'Y' 'unit' => 'Y'
]; ];
} }
throw new TransformationFailedException('the date interval does not ' throw new TransformationFailedException(
. 'contains any days, months or years'); 'The date interval does not contains any days, months or years.'
);
} }
public function reverseTransform($value) public function reverseTransform($value)
{ {
if (empty($value) or empty($value['n'])) { if (empty($value) || empty($value['n'])) {
return null; return null;
} }
@ -83,10 +65,11 @@ class DateIntervalTransformer implements DataTransformerInterface
try { try {
return new \DateInterval($string); return new \DateInterval($string);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new TransformationFailedException("Could not transform value " throw new TransformationFailedException(
. "into DateInterval", 1542, $e); 'Could not transform value into DateInterval',
} 1542,
$e
);
}
} }
} }