Skip to content

Commit

Permalink
New. Heristic improvement. Convert file_get_contents() to gathered …
Browse files Browse the repository at this point in the history
…content string.
  • Loading branch information
Glomberg authored May 7, 2024
1 parent 825b339 commit 2264094
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions HeuristicAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public function processContent()
$this->mathematics->evaluateMathExpressions();
$this->strings->convertToSimple($key);
$this->strings->convertChrFunctionToString($key);
$this->strings->convertFileGetContentsToString($this->path);
}

foreach ( $this->tokens as $key => $_current_token ) {
Expand Down
58 changes: 58 additions & 0 deletions Modules/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,64 @@ static function ($elem) {
return false;
}

/**
* Convert file_get_contents(__DIR__ . '/file.example') to gathered content string
*
* @param string $current_file_path
* @return void
*/
public function convertFileGetContentsToString($current_file_path)
{
if (
$this->tokens->current->type === 'T_STRING' &&
$this->tokens->current->value === 'file_get_contents' &&
$this->tokens->next1->value === '('
) {
$start_position = $this->tokens->next1[3];
$closing_bracket_position = $this->tokens->searchForward($start_position, ')');
$tokens_inside_brackets = $this->tokens->getRange($start_position + 1, $closing_bracket_position - 1);

// Check against of nested bracers.
// @ToDo implement nested bracers values calculating
$is_nested_bracers = false;
foreach ( $tokens_inside_brackets as $token ) {
if ( $token->value === '(' ) {
$is_nested_bracers = true;
}
}

if ( $is_nested_bracers ) {
return;
}

// Calculate path string
$path = '';
foreach ($tokens_inside_brackets as $token) {
if ( $token->isTypeOf('could_be_concatenated') ) {
$path .= trim((string)$token->value, '\'');
}
if ( $token->type === 'T_DIR' ) {
$path .= dirname($current_file_path);
}
}

if ( $path && file_exists($path) ) {
// Delete tokens which contained the file_get_contents expression
for ( $i = $start_position; $i <= $closing_bracket_position; $i++ ) {
$this->tokens->unsetTokens($i);
}

// Insert newly calculated token with gathered content string
$this->tokens['current'] = new Token(
'T_LNUMBER',
@file_get_contents($path),
$this->tokens->current->line,
$this->tokens->current->key
);
}
}
}

/**
* Concatenates simple strings with type T_CONSTANT_ENCAPSED_STRING
*
Expand Down

0 comments on commit 2264094

Please sign in to comment.