Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VariableParser and VariableContext #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

N1ebieski
Copy link

This PR adds support for VariableParser and VariableContext.

For example, currently, this command:

php-parser detect "<?php

Gate::authorize('edit', \$user);" --debug

returns:

================================================================================
================================================================================
                              STARTING TO PARSE
================================================================================
================================================================================

Microsoft\PhpParser\Node\SourceFileNode <?php Gate::authorize('edit', $user);
+ Context: App\Contexts\Base
* Parsing: App\Parsers\SourceFileNodeParser

 Microsoft\PhpParser\Node\Statement\InlineHtml <?php 
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\InlineHtmlParser

 Microsoft\PhpParser\Node\Statement\ExpressionStatement Gate::authorize('edit', $user);
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\ExpressionStatementParser

  Microsoft\PhpParser\Node\Expression\CallExpression Gate::authorize('edit', $user)
  + Context: App\Contexts\MethodCall
  * Parsing: App\Parsers\CallExpressionParser

   Microsoft\PhpParser\Node\Expression\ScopedPropertyAccessExpression Gate::authorize
   + Context: App\Contexts\MethodCall
   * Parsing: App\Parsers\ScopedPropertyAccessExpressionParser

    Microsoft\PhpParser\Node\QualifiedName Gate
   Microsoft\PhpParser\Node\DelimitedList\ArgumentExpressionList 'edit', $user
   + Context: App\Contexts\MethodCall
   * Parsing: App\Parsers\ArgumentExpressionListParser

    Microsoft\PhpParser\Node\Expression\ArgumentExpression 'edit'
    + Context: App\Contexts\Argument
    * Parsing: App\Parsers\ArgumentExpressionParser

     Microsoft\PhpParser\Node\StringLiteral 'edit'
     + Context: App\Contexts\StringValue
     * Parsing: App\Parsers\StringLiteralParser

    Microsoft\PhpParser\Node\Expression\ArgumentExpression $user
    + Context: App\Contexts\Argument
    * Parsing: App\Parsers\ArgumentExpressionParser

     Microsoft\PhpParser\Node\Expression\Variable $user
[
    {
        "type": "methodCall",
        "methodName": "authorize",
        "className": "Gate",
        "arguments": {
            "type": "arguments",
            "autocompletingIndex": 2,
            "children": [
                {
                    "type": "argument",
                    "name": null,
                    "children": [
                        {
                            "type": "string",
                            "value": "edit",
                            "start": {
                                "line": 2,
                                "column": 16
                            },
                            "end": {
                                "line": 2,
                                "column": 20
                            }
                        }
                    ]
                },
                {
                    "type": "argument",
                    "name": null,
                    "children": []
                }
            ]
        }
    }
]

With VariableParser and VariableContext:

================================================================================
================================================================================
                              STARTING TO PARSE
================================================================================
================================================================================

Microsoft\PhpParser\Node\SourceFileNode <?php Gate::authorize('edit', $user);
+ Context: App\Contexts\Base
* Parsing: App\Parsers\SourceFileNodeParser

 Microsoft\PhpParser\Node\Statement\InlineHtml <?php 
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\InlineHtmlParser

 Microsoft\PhpParser\Node\Statement\ExpressionStatement Gate::authorize('edit', $user);
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\ExpressionStatementParser

  Microsoft\PhpParser\Node\Expression\CallExpression Gate::authorize('edit', $user)
  + Context: App\Contexts\MethodCall
  * Parsing: App\Parsers\CallExpressionParser

   Microsoft\PhpParser\Node\Expression\ScopedPropertyAccessExpression Gate::authorize
   + Context: App\Contexts\MethodCall
   * Parsing: App\Parsers\ScopedPropertyAccessExpressionParser

    Microsoft\PhpParser\Node\QualifiedName Gate
   Microsoft\PhpParser\Node\DelimitedList\ArgumentExpressionList 'edit', $user
   + Context: App\Contexts\MethodCall
   * Parsing: App\Parsers\ArgumentExpressionListParser

    Microsoft\PhpParser\Node\Expression\ArgumentExpression 'edit'
    + Context: App\Contexts\Argument
    * Parsing: App\Parsers\ArgumentExpressionParser

     Microsoft\PhpParser\Node\StringLiteral 'edit'
     + Context: App\Contexts\StringValue
     * Parsing: App\Parsers\StringLiteralParser

    Microsoft\PhpParser\Node\Expression\ArgumentExpression $user
    + Context: App\Contexts\Argument
    * Parsing: App\Parsers\ArgumentExpressionParser

     Microsoft\PhpParser\Node\Expression\Variable $user
     + Context: App\Contexts\Variable
     * Parsing: App\Parsers\VariableParser

[
    {
        "type": "methodCall",
        "methodName": "authorize",
        "className": "Gate",
        "arguments": {
            "type": "arguments",
            "autocompletingIndex": 2,
            "children": [
                {
                    "type": "argument",
                    "name": null,
                    "children": [
                        {
                            "type": "string",
                            "value": "edit",
                            "start": {
                                "line": 2,
                                "column": 16
                            },
                            "end": {
                                "line": 2,
                                "column": 20
                            }
                        }
                    ]
                },
                {
                    "type": "argument",
                    "name": null,
                    "children": [
                        {
                            "type": "variable",
                            "name": "user",
                            "start": {
                                "line": 2,
                                "column": 24
                            },
                            "end": {
                                "line": 2,
                                "column": 29
                            }
                        }
                    ]
                }
            ]
        }
    }
]

In the future, this can be extended to include reading docblocks to retrieve value.

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