Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8344343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:20:29+00:00 2026-06-09T06:20:29+00:00

Is there any way in PHP to do static code analysis and detect reliance

  • 0

Is there any way in PHP to do static code analysis and detect reliance on the register_globals initiative? It’s relatively straightforward to manually examine a file and look for variables which have not been initialized and infer from that that these may be relying on it, but I need to do this for many hundreds of scripts, so I’m looking for an automated solution.

My last resort is setting up a dev environment with the directive turned off and strict error reporting and letting QA play around for a long while, then fix the instances that the error log catches, but this is not guaranteed to find 100% of the cases, and certainly not a good use of resources if an automated solution exists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:20:33+00:00Added an answer on June 9, 2026 at 6:20 am

    A small script I just hacked together to detect simple undefined variables. You’ll need PHP-Parser for this:

    <?php
    
    error_reporting(E_ALL);
    
    $dir = './foo';
    
    require_once './lib/bootstrap.php';
    
    class Scope {
        protected $stack;
        protected $pos;
    
        public function __construct() {
            $this->stack = array();
            $this->pos = -1;
        }
    
        public function addVar($name) {
            $this->stack[$this->pos][$name] = true;
        }
    
        public function hasVar($name) {
            return isset($this->stack[$this->pos][$name]);
        }
    
        public function pushScope() {
            $this->stack[++$this->pos] = array();
        }
    
        public function popScope() {
            --$this->pos;
        }
    }
    
    class UndefinedVariableVisitor extends PHPParser_NodeVisitorAbstract {
        protected $scope;
        protected $parser;
        protected $traverser;
    
        public function __construct(Scope $scope, PHPParser_Parser $parser, PHPParser_NodeTraverser $traverser) {
            $this->scope = $scope;
            $this->parser = $parser;
            $this->traverser = $traverser;
        }
    
        public function enterNode(PHPParser_Node $node) {
            if (($node instanceof PHPParser_Node_Expr_Assign || $node instanceof PHPParser_Node_Expr_AssignRef)
                && $node->var instanceof PHPParser_Node_Expr_Variable
                && is_string($node->var->name)
            ) {
                $this->scope->addVar($node->var->name);
            } elseif ($node instanceof PHPParser_Node_Stmt_Global || $node instanceof PHPParser_Node_Stmt_Static) {
                foreach ($node->vars as $var) {
                    if (is_string($var->name)) {
                        $this->scope->addVar($var->name);
                    }
                }
            } elseif ($node instanceof PHPParser_Node_Expr_Variable && is_string($node->name)) {
                if (!$this->scope->hasVar($node->name)) {
                    echo 'Undefined variable $' . $node->name . ' on line ' . $node->getLine() . "\n";
                }
            } elseif ($node instanceof PHPParser_Node_Stmt_Function || $node instanceof PHPParser_Node_Stmt_ClassMethod) {
                $this->scope->pushScope();
    
                // params are always available
                foreach ($node->params as $param) {
                    $this->scope->addVar($param->name);
                }
    
                // methods always have $this
                if ($node instanceof PHPParser_Node_Stmt_ClassMethod) {
                    $this->scope->addVar('this');
                }
            } elseif ($node instanceof PHPParser_Node_Expr_Include && $node->expr instanceof PHPParser_Node_Scalar_String) {
                $file = $node->expr->value;
                $code = file_get_contents($file);
                $stmts = $this->parser->parse($code);
    
                // for includes within the file
                $cwd = getcwd();
                chdir(dirname($file));
    
                $this->traverser->traverse($stmts);
    
                chdir($cwd);
            }
        }
    
        public function leaveNode(PHPParser_Node $node) {
            if ($node instanceof PHPParser_Node_Stmt_Function || $node instanceof PHPParser_Node_Stmt_ClassMethod) {
                $this->scope->popScope();
            }
        }
    }
    
    $parser = new PHPParser_Parser(new PHPParser_Lexer());
    
    $scope = new Scope;
    
    $traverser = new PHPParser_NodeTraverser;
    $traverser->addVisitor(new UndefinedVariableVisitor($scope, $parser, $traverser));
    
    foreach (new RecursiveIteratorIterator(
                 new RecursiveDirectoryIterator($dir),
                 RecursiveIteratorIterator::LEAVES_ONLY)
             as $file
    ) {
        if (!preg_match('/\.php$/', $file)) continue;
    
        echo 'Checking ' . $file . ':', "\n";
    
        $code = file_get_contents($file);
        $stmts = $parser->parse($code);
    
        // for includes within the file
        $cwd = getcwd();
        chdir(dirname($file));
    
        $scope->pushScope();
        $traverser->traverse($stmts);
        $scope->popScope();
    
        chdir($cwd);
    
        echo "\n";
    }
    

    It’s just a very basic implementation and I did not test it extensively, but it should work for scripts that don’t go wild with $GLOBALS and $$varVars. It does basic include resolution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to include php file using require and calling a php
Is there any way to call a php class (eg. $var = new className)
Is there any way to upload an entire folder using PHP?
Is there any way to differentiate IE7 versus IE6 using PHP's get_browser() function?
Is there any way that I could enjoy a decodeValue() function in PHP, too?
Is there any way to remove the contents of an file in php, do
Is there any way to get a certain thumbnail generated by PHP from a
If yes is there any way to access a var defined in another PHP
Quick one; Netbeans 7.0 for PHP development: Is there any way to inform NetBeans
Is there any way to pass variable to the set_exception_handler() method in PHP? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.