I have this function:
public static function findFirstSymbolPosition(
string $haystack,
string $needle,
$skipFromChars = '',
$skipUntilChars = ''
): int|false {
// Edge case: If the string starts with the symbol, then the array count of splitting the elements will be 1
if (substr($haystack, 0, strlen($needle)) == $needle) {
return 0;
}
// Split on that searching element: If it appears within the string,
// it will produce an array with exactly 2 elements (since using option "ONLY_FIRST_OCCURRENCE")
// The length of the first element equals the position of that symbol
$fieldQueryInterpreter = QueryParserFacade::getInstance();
$options = [
QueryParserOptions::ONLY_FIRST_OCCURRENCE => true,
];
$symbolElems = $fieldQueryInterpreter->splitElements(
$haystack,
$needle,
$skipFromChars,
$skipUntilChars,
QuerySyntax::SYMBOL_FIELDARGS_ARGVALUESTRING_OPENING,
QuerySyntax::SYMBOL_FIELDARGS_ARGVALUESTRING_CLOSING,
$options
);
if (count($symbolElems) == 2) {
return strlen($symbolElems[0]);
}
// Edge case: If the string finishes with the symbol, then the array count of splitting the elements will be 1
if (substr($haystack, -1 * strlen($needle)) == $needle) {
return strlen($haystack) - strlen($needle);
}
return false;
}
It must return false
if the string is not found, or an int
with the first position where it is found.
When transpiled to 7.1, it becomes this:
public static function findFirstSymbolPosition(string $haystack, string $needle, $skipFromChars = '', $skipUntilChars = '')
{
// Edge case: If the string starts with the symbol, then the array count of splitting the elements will be 1
if (\Phabel\Target\Php80\Polyfill::substr($haystack, 0, strlen($needle)) == $needle) {
$phabelReturn = 0;
if (!$phabelReturn instanceof false) {
if (!\is_int($phabelReturn)) {
if (!(\is_bool($phabelReturn) || \is_numeric($phabelReturn))) {
throw new \TypeError(__METHOD__ . '(): Return value must be of type false|int, ' . \Phabel\Plugin\TypeHintReplacer::getDebugType($phabelReturn) . ' returned in ' . \Phabel\Plugin\TypeHintReplacer::trace());
} else {
$phabelReturn = (int) $phabelReturn;
}
}
}
return $phabelReturn;
}
// Split on that searching element: If it appears within the string,
// it will produce an array with exactly 2 elements (since using option "ONLY_FIRST_OCCURRENCE")
// The length of the first element equals the position of that symbol
$fieldQueryInterpreter = QueryParserFacade::getInstance();
$options = [QueryParserOptions::ONLY_FIRST_OCCURRENCE => true];
$symbolElems = $fieldQueryInterpreter->splitElements($haystack, $needle, $skipFromChars, $skipUntilChars, QuerySyntax::SYMBOL_FIELDARGS_ARGVALUESTRING_OPENING, QuerySyntax::SYMBOL_FIELDARGS_ARGVALUESTRING_CLOSING, $options);
if (count($symbolElems) == 2) {
$phabelReturn = strlen($symbolElems[0]);
if (!$phabelReturn instanceof false) {
if (!\is_int($phabelReturn)) {
if (!(\is_bool($phabelReturn) || \is_numeric($phabelReturn))) {
throw new \TypeError(__METHOD__ . '(): Return value must be of type false|int, ' . \Phabel\Plugin\TypeHintReplacer::getDebugType($phabelReturn) . ' returned in ' . \Phabel\Plugin\TypeHintReplacer::trace());
} else {
$phabelReturn = (int) $phabelReturn;
}
}
}
return $phabelReturn;
}
// Edge case: If the string finishes with the symbol, then the array count of splitting the elements will be 1
if (\Phabel\Target\Php80\Polyfill::substr($haystack, -1 * strlen($needle)) == $needle) {
$phabelReturn = strlen($haystack) - strlen($needle);
if (!$phabelReturn instanceof false) {
if (!\is_int($phabelReturn)) {
if (!(\is_bool($phabelReturn) || \is_numeric($phabelReturn))) {
throw new \TypeError(__METHOD__ . '(): Return value must be of type false|int, ' . \Phabel\Plugin\TypeHintReplacer::getDebugType($phabelReturn) . ' returned in ' . \Phabel\Plugin\TypeHintReplacer::trace());
} else {
$phabelReturn = (int) $phabelReturn;
}
}
}
return $phabelReturn;
}
$phabelReturn = false;
if (!$phabelReturn instanceof false) {
if (!\is_int($phabelReturn)) {
if (!(\is_bool($phabelReturn) || \is_numeric($phabelReturn))) {
throw new \TypeError(__METHOD__ . '(): Return value must be of type false|int, ' . \Phabel\Plugin\TypeHintReplacer::getDebugType($phabelReturn) . ' returned in ' . \Phabel\Plugin\TypeHintReplacer::trace());
} else {
$phabelReturn = (int) $phabelReturn;
}
}
}
return $phabelReturn;
}
There is a problem: the false
would always be converted to 0
, breaking the application, in this part:
$phabelReturn = false;
if (!$phabelReturn instanceof false) {
if (!\is_int($phabelReturn)) {
if (!(\is_bool($phabelReturn) || \is_numeric($phabelReturn))) {
throw new \TypeError(__METHOD__ . '(): Return value must be of type false|int, ' . \Phabel\Plugin\TypeHintReplacer::getDebugType($phabelReturn) . ' returned in ' . \Phabel\Plugin\TypeHintReplacer::trace());
} else {
$phabelReturn = (int) $phabelReturn;
}
}
}
return $phabelReturn;
After commenting line $phabelReturn = (int) $phabelReturn;
it started working.
Indeed, several sections of my application were not working after transpiling to 7.. Then I commented all instances of these pieces of code:
// $phabelReturn = (string) $phabelReturn;
// $phabelReturn = (int) $phabelReturn;
// $phabelReturn = (bool) $phabelReturn;
Then it started working.
Maybe the issue is returning false|int
? Or maybe casting shouldn't be done? (Maybe there's no need for it?)