<commit_before><?php

require __DIR__."/../vendor/autoload.php";

use PhpParser\Error;
use PhpParser\ParserFactory;
use PhpParser\NodeVisitorAbstract;
use PhpParser\NodeTraverser;
use PhpParser\Node;
use PhpParser\Node\Stmt;

class MyParserNodeVisitor extends \PhpParser\NodeVisitorAbstract
{
    function __construct() {
        
    }
    
    public function enterNode(Node $node)
    {
        /*if ($node instanceof Node\Name)
        {
             //echo "Node nameD:". $node."\n\n";
            return new Node\Name($node->toString('_'));
        }*/
        
        if ( $node instanceof Stmt\Interface_
        || $node instanceof Stmt\Function_)
        {
            echo "\nNode name:". $node->name."\n\n";
            
            print_r($node);
            
            //$node->name = $node->namespacedName->toString('_');
        }
        
        elseif ($node instanceof Stmt\Trait_)
        {
            
            //List all implements
            
            //List all classes
            
            //This class can use those functions as own
            //Parse and get those functions also
            
            
            echo "\Trait name:". $node->name."\n\n";
            
        }
        elseif ($node instanceof Stmt\TraitUse)
        {
/*            echo "\Trait name:". $node->traits."\n\n";*/
            
            foreach ($node->traits as $trait) {
            	print_r($trait);
            }
            
        }
        elseif ($node instanceof Stmt\Class_)
        {
            
            //List all implements
            
            //List all classes
            
            //This class can use those functions as own
            //Parse and get those functions also
            
            
            echo "\nClass name:". $node->name."\n\n";
            
        }
        
        elseif ($node instanceof Stmt\ClassMethod)
        {
            echo "\nClass method:". $node->name."\n\n";
        }
        elseif ($node instanceof Stmt\Const_)
        {
            /*foreach ($node->consts as $const) {
                $const->name = $const->namespacedName->toString('_');
            }*/
        }
        elseif ($node instanceof Stmt\Namespace_)
        {
            echo "\nNode Namespace_ name:". $node->name."\n\n";
            // returning an array merges is into the parent array
            //return $node->stmts;
        }
        elseif ($node instanceof Stmt\Use_)
        {
            //print_r($node);
            echo "\nNode Use_ Namespace_ name:";//. $node->uses[0]->name."\n\n";
            
            foreach($node->uses as $use)
            {
                
                //Name is using _ as seperator
                
                echo $use->name. " | " .$use->alias. "\n\n";
            }
            
            // returning false removed the node altogether
            //return false;
        }
    }
}



$lexer = new PhpParser\Lexer(array(
'usedAttributes' => array(
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'
)
));

$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP5, $lexer);

$visitor = new MyParserNodeVisitor();

$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor($visitor);

$filename = $argv[1];

if(empty($filename))
exit();

try {
    $stmts = $parser->parse(file_get_contents($filename));
    
    print_r($stmts);
    
    //$visitor->setTokens($lexer->getTokens());
    $stmts = $traverser->traverse($stmts);
    
}
catch (PhpParser\Error $e)
{<commit_msg>creating json<commit_after><?php

require __DIR__."/../vendor/autoload.php";

use PhpParser\Error;
use PhpParser\ParserFactory;
use PhpParser\NodeVisitorAbstract;
use PhpParser\NodeTraverser;
use PhpParser\Node;
use PhpParser\Node\Stmt;

define("CACHE_DIR", "_kiwicache_" . DIRECTORY_SEPARATOR );

class JsonHelper
{
    public static function createCache()
    {
        if(!is_dir(CACHE_DIR))
            mkdir( CACHE_DIR , 0777, true);
    }
    
    public static function writeJson($filename, $result)
    {
        $filename = CACHE_DIR . basename($filename,".php");

        echo "Writing json file -> " .$filename.PHP_EOL ;
        
        $content = json_encode($result);
        
        echo "content: " . $content.PHP_EOL;

        $fp = fopen($filename.'.json', 'w');
        
        fwrite($fp, $content );
        
        fclose($fp);
    }
}

class MyParserNodeVisitor extends \PhpParser\NodeVisitorAbstract
{
    
    private $result;
    private $filename;
    
    function __construct($filename) 
    {
        $this->filename = $filename;
    }
    
    
    
    public function enterNode(Node $node)
    {
        /*if ($node instanceof Node\Name)
        {
             //echo "Node nameD:". $node."\n\n";
            return new Node\Name($node->toString('_'));
        }*/
        
        if ( $node instanceof Stmt\Interface_
        || $node instanceof Stmt\Function_)
        {
            echo "\nNode name:". $node->name."\n\n";
            
            //print_r($node);
            
            //$node->name = $node->namespacedName->toString('_');
        }
        
        elseif ($node instanceof Stmt\Trait_)
        {
            
            //List all implements
            
            //List all classes
            
            //This class can use those functions as own
            //Parse and get those functions also
            
            
            echo "\Trait name:". $node->name."\n\n";
            
        }
        elseif ($node instanceof Stmt\TraitUse)
        {
            /*            echo "\Trait name:". $node->traits."\n\n";*/
            
            foreach ($node->traits as $trait) {
                //print_r($trait);
            }
            
        }
        elseif ($node instanceof Stmt\Class_)
        {
            
            //List all implements
            
            //List all classes
            
            //This class can use those functions as own
            //Parse and get those functions also
            
            
            echo "\nClass name:". $node->name."\n\n";
            
            $this->result['class'] = $node->name;
            
        }
        
        elseif ($node instanceof Stmt\ClassMethod)
        {
            echo "\nClass method:". $node->name."\n\n";
        }
        elseif ($node instanceof Stmt\Const_)
        {
            /*foreach ($node->consts as $const) {
                $const->name = $const->namespacedName->toString('_');
            }*/
        }
        elseif ($node instanceof Stmt\Namespace_)
        {
            echo "\nNode Namespace_ name:". $node->name."\n\n";
            // returning an array merges is into the parent array
            //return $node->stmts;
        }
        elseif ($node instanceof Stmt\Use_)
        {
            //print_r($node);
            echo "\nNode Use_ Namespace_ name:";//. $node->uses[0]->name."\n\n";
            
            foreach($node->uses as $use)
            {
                
                //Name is using _ as seperator
                
                echo $use->name. " | " .$use->alias. "\n\n";
            }
            
            // returning false removed the node altogether
            //return false;
        }
    }
    
    public function afterTraverse(array $nodes)
    {
        
        //Write a file with some node value
        JsonHelper::writeJson($this->filename,$this->result);
    }
}



$lexer = new PhpParser\Lexer(array(
'usedAttributes' => array(
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'
)
));

//Create cache dir

JsonHelper::createCache();

$filename = $argv[1];

$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP5, $lexer);

$visitor = new MyParserNodeVisitor($filename);

$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor($visitor);


if(empty($filename))
exit();

try {
    $stmts = $parser->parse(file_get_contents($filename));
    
    //print_r($stmts);
    
    //$visitor->setTokens($lexer->getTokens());
    $stmts = $traverser->traverse($stmts);
    
}
catch (PhpParser\Error $e)
{
{'fraction_non_alphanumeric': 0.13371666258869586, 'fraction_numerical': 0.001223391240518718, 'mean_word_length': 1.4969456322541235, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6224480', 'n_tokens_mistral': 2377, 'n_tokens_neox': 2369, 'n_words': 509}