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

New. Heristic improvement. Convert file_get_contents() to gathered content string. #3

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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