Skip to content

Added flow sensitive analysis capabilities#1069

Merged
romalytvynenko merged 46 commits intomainfrom
feat/more-inference-data-in-scope-2
Jan 28, 2026
Merged

Added flow sensitive analysis capabilities#1069
romalytvynenko merged 46 commits intomainfrom
feat/more-inference-data-in-scope-2

Conversation

@romalytvynenko
Copy link
Member

@romalytvynenko romalytvynenko commented Jan 26, 2026

This PR adds capabilities for flow sensitive analysis:

  • Scramble now builds CFG for methods/functions
  • Implemented a simple API for querying the CFG to find value origins and expression types at a flow node.

To retrieve the instance of CGF (specifically flow container), you can call getFlowContainer on method definition:

$classDefinition = app(Infer::class)->analyzeClass(UserResource::class);

$flow = $classDefinition->getMethod('response')->getFlowContainer();

On flow container instance currently there are 2 methods considered as public API.

  • findValueOriginsByExitType (callable(Type): bool): Flow\Node[] - allows to find the nodes where some specific exit values created by the type.

For example, this function returns 42|1:

function foo () {
    $a = 1;
    if (mt_rand(0, 1) === 42) {
        $a = 42;
    }
    return $a;
}

You can find $a = 42 flow node by using findValueOriginsByExitType:

use Dedoc\Scramble\Support\Type\Literal\LiteralIntegerType;

$nodes = $flowContainer->findValueOriginsByExitType(
    fn (Type $t) => $t instanceof LiteralIntegerType && $t->value === 42,
);
  • getTypeAt (Expr, Flow\Node) - allows to get the type of the expression at a given flow node. This will also take conditional type narrows in consideration, currently Identical checks are supported:
function foo () {
    $b = mt_rand(0, 100);
    if ($b === 42) {
        // when checking `$b` type here, Scramble will know it is `42`.
    }
    return 0;
}

@romalytvynenko romalytvynenko force-pushed the feat/more-inference-data-in-scope-2 branch from a3b2590 to ebb374f Compare January 28, 2026 11:49
@romalytvynenko romalytvynenko changed the title Added flow sensitive analysis and API for it Added flow sensitive analysis capabilities Jan 28, 2026
@romalytvynenko romalytvynenko merged commit 5f1e692 into main Jan 28, 2026
1 of 17 checks passed
@romalytvynenko romalytvynenko deleted the feat/more-inference-data-in-scope-2 branch January 28, 2026 14:01
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.

1 participant