Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Dec 1, 2025

as in

  • public function getUnaryMinusType(Expr $expr, callable $getTypeCallback): Type
    {
    $type = $getTypeCallback($expr)->toNumber();
    $scalarValues = $type->getConstantScalarValues();
    if (count($scalarValues) > 0) {
    $newTypes = [];
    foreach ($scalarValues as $scalarValue) {
    if (is_int($scalarValue)) {
    /** @var int|float $newValue */
    $newValue = -$scalarValue;
    if (!is_int($newValue)) {
    return $type;
    }
    $newTypes[] = new ConstantIntegerType($newValue);
    } elseif (is_float($scalarValue)) {
    $newTypes[] = new ConstantFloatType(-$scalarValue);
    }
    }
    return TypeCombinator::union(...$newTypes);
    }
    if ($type instanceof IntegerRangeType) {
    return $getTypeCallback(new Expr\BinaryOp\Mul($expr, new Int_(-1)));
    }
    return $type;
    }
  • } elseif (
    $expr instanceof Expr\BitwiseNot
    || $expr instanceof Cast
    || $expr instanceof Expr\Clone_
    || $expr instanceof Expr\UnaryMinus
    || $expr instanceof Expr\UnaryPlus
    ) {
    $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep());
    $throwPoints = $result->getThrowPoints();
    $impurePoints = $result->getImpurePoints();
    $isAlwaysTerminating = $result->isAlwaysTerminating();
    $hasYield = $result->hasYield();
    $scope = $result->getScope();
    } elseif ($expr instanceof Expr\Eval_) {

Comment on lines 44 to 45
$type = $mulResult->result;
$nativeType = $mulResult->nativeType;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why PHPStan reports

 ------ ----------------------------------------------------------------------------------------- 
  Line   src/Analyser/Generator/ExprHandler/UnaryMinusHandler.php                                 
 ------ ----------------------------------------------------------------------------------------- 
  44     Access to an undefined property PHPStan\Analyser\Generator\ExprAnalysisResult::$result.  
         🪪  property.notFound                                                                    
         💡  Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property    
         at src/Analyser/Generator/ExprHandler/UnaryMinusHandler.php:44                           
 ------ -----------------------------------------------------------------------------------------

here but it does not on line 40.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean. The reported lines are sometimes shifted because code is downgraded on lower PHP versions.

But $mulResult->result is really an error. It's supposed to be $mulResult->type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha, seems it was too early this morning ;).

hopefully we can make improvements so phpstorm autocomplete and type-discovery work better with this fiber code. atm it feels kind of unsupported.

are you aware whether there are already open issues on the phpstorm bugtracker to improve on type stuff with Fibers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the handlers it's not about fibers but about generators. I think it's hard and no syntax was yet developed for this. We'd need something for @return Generator<... that would tell PHPStan + PhpStorm "when this is yielded, this is sent".

Psalm has @psalm-yield annotation but it doesn't make sense to me. Because what is sent depends on context. For the same yielded objects, different things can get sent to it in different generators.

@staabm staabm marked this pull request as ready for review December 1, 2025 09:02
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes ondrejmirtes merged commit d8fdb61 into phpstan:2.1.x Dec 1, 2025
608 of 638 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the unary-m branch December 1, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants