PHP code example of permafrost-dev / php-code-search

1. Go to this page and download the library: Download permafrost-dev/php-code-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

permafrost-dev / php-code-search example snippets


use Permafrost\PhpCodeSearch\Searcher;

$searcher = new Searcher();

$searcher
    ->functions(['strtolower', 'strtoupper'])
    ->search('./file1.php');

$searcher
    ->variables(['/^one[A-Z]$/'])
    ->searchCode(' $oneA = "1a";');

$results = $searcher
    ->variables(['twoA', '/^one.$/'])
    ->searchCode(' '.
    '    $oneA = "1a";'.
    '    $oneB = "1b";'.
    '    $twoA = "2a";'.
    '    $twoB = "2b";'.
    '');
    
foreach($results->results as $result) {
    echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
}

// search for references AND definitions for 'strtolower' and/or 'myfunc'
$searcher
    ->functions(['strtolower', 'myfunc'])
    ->search('file1.php');

$results = $searcher
    ->methods(['/test(One|Two)/'])
    ->searchCode(' '.
      '    $obj->testOne("hello world 1"); '.
      '    $obj->testTwo("hello world", 2); '.
      ''
    );
    
foreach($results->results as $result) {
    echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;

    foreach($result->node->args as $arg) {
        echo "  argument: '{$arg->value}'" . PHP_EOL;
    }
}

$searcher
    ->static(['Ray', 'Cache::has', 'Request::$myProperty'])
    ->search('./app/Http/Controllers/MyController.php');

$searcher
    ->classes(['MyController'])
    ->search('./app/Http/Controllers/MyController.php');

$searcher
    ->assignments(['myVar'])
    ->search('./app/Http/Controllers/MyController.php');

$searcher
    ->withoutSnippets()
    ->functions(['strtolower'])
    ->search('file1.php');