Skip to content

Commit 2ff80ba

Browse files
niconoe-ondrejmirtes
authored andcommitted
Simplify method canStringBeSanitized.
1 parent e4a777d commit 2ff80ba

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

‎src/Type/Php/FilterFunctionReturnTypeHelper.php‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType, Ph
185185
$type = $this->applyRangeOptions($type, $options, $defaultType);
186186

187187
if (
188-
$inputType->isNonEmptyString()->yes()
188+
!$this->canStringBeSanitized($filterValue, $flagsType)
189+
&& $inputType->isNonEmptyString()->yes()
189190
&& $type->isString()->yes()
190-
&& $this->canStringBeSanitized($filterValue, $flagsType)->no()
191191
) {
192192
$accessory = new AccessoryNonEmptyStringType();
193193
if ($inputType->isNonFalsyString()->yes()) {
@@ -410,7 +410,7 @@ private function determineExactType(Type $in, int $filterValue, Type $defaultTyp
410410
$useDefaultType = true;
411411
}
412412

413-
if ($this->canStringBeSanitized($filterValue, $flagsType)->no()) {
413+
if (!$this->canStringBeSanitized($filterValue, $flagsType)) {
414414
$stringType = $inputType->toString();
415415
} else {
416416
$stringType = TypeCombinator::union(
@@ -586,22 +586,27 @@ private function getFlagsValue(Type $exprType): Type
586586
);
587587
}
588588

589-
private function canStringBeSanitized(int $filterValue, ?Type $flagsType): TrinaryLogic
589+
private function canStringBeSanitized(int $filterValue, ?Type $flagsType): bool
590590
{
591591
// If it is a validation filter, the string will not be changed
592592
if ($this->isValidationFilter($filterValue)) {
593-
return TrinaryLogic::createNo();
593+
return false;
594594
}
595595

596596
// FILTER_DEFAULT will not sanitize, unless it has FILTER_FLAG_STRIP_LOW,
597597
// FILTER_FLAG_STRIP_HIGH, or FILTER_FLAG_STRIP_BACKTICK
598598
if ($filterValue === $this->getConstant('FILTER_DEFAULT')) {
599-
return $this->hasFlag('FILTER_FLAG_STRIP_LOW', $flagsType)
599+
$hasStripFlags = $this->hasFlag('FILTER_FLAG_STRIP_LOW', $flagsType)
600600
->or($this->hasFlag('FILTER_FLAG_STRIP_HIGH', $flagsType))
601601
->or($this->hasFlag('FILTER_FLAG_STRIP_BACKTICK', $flagsType));
602+
if ($hasStripFlags->maybe()) {
603+
// Too complicated, considering strip flags are not defined (default behavior).
604+
return false;
605+
}
606+
return $hasStripFlags->yes();
602607
}
603608

604-
return TrinaryLogic::createYes();
609+
return true;
605610
}
606611

607612
private function isValidationFilter(int $filterValue): bool

0 commit comments

Comments
 (0)