rector icon indicating copy to clipboard operation
rector copied to clipboard

System error: "Syntax error, unexpected T_PUBLIC:1

Open blue928 opened this issue 2 years ago • 3 comments

Bug Report

Hello,

My use of rector threw an error, and the error itself asked me to open an issue here. Here are the details:

Rector version 0.14.0

[ERROR] Could not process "sites/all/modules/custom/fc_search_support/fc_search_support.module" file,  
         due to:                                                                                        
         "System error: "Syntax error, unexpected T_PUBLIC:1"                                           
         Run Rector with "--debug" option and post the report here:                                     
         https://github.com/rectorphp/rector/issues/new". On line: 269     

with --debug

[ERROR] Could not process "sites/all/modules/custom/fc_search_support/fc_search_support.module" file, due to: 
         "System error: "Syntax error, unexpected T_PUBLIC:1"                                           
                                                                                                        
         Stack trace:                                                                                   
         #0                                                                                             
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php(143):
         PhpParser\ParserAbstract->doParse()                                                            
         #1                                                                                             
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/Parser/Multiple.php(48):
         PhpParser\ParserAbstract->parse('<?php public://...', Object(PhpParser\ErrorHandler\Throwing)) 
         #2                                                                                             
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/Parser/Multiple.php(31):
         PhpParser\Parser\Multiple->tryParse(Object(PhpParser\Parser\Php7),                             
         Object(PhpParser\ErrorHandler\Throwing), '<?php public://...')                                 
         #3 /var/www/.composer/vendor/rector/rector/src/PhpParser/Parser/SimplePhpParser.php(43):       
         PhpParser\Parser\Multiple->parse('<?php public://...')                                         
         #4 /var/www/.composer/vendor/rector/rector/rules/Php72/NodeFactory/AnonymousFunctionFactory.php(171):
         Rector\Core\PhpParser\Parser\SimplePhpParser->parseString('<?php public://...')                
         #5                                                                                             
         /var/www/.composer/vendor/rector/rector/rules/Php55/Rector/FuncCall/PregReplaceEModifierRector.php(103):
         Rector\Php72\NodeFactory\AnonymousFunctionFactory->createAnonymousFunctionFromExpr(Object(PhpParser\Nod
         e\Scalar\String_))                                                                             
         #6 /var/www/.composer/vendor/rector/rector/src/Rector/AbstractRector.php(206):                 
         Rector\Php55\Rector\FuncCall\PregReplaceEModifierRector->refactor(Object(PhpParser\Node\Expr\FuncCall))
         #7 /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(113):
         Rector\Core\Rector\AbstractRector->enterNode(Object(PhpParser\Node\Expr\FuncCall))             
         #8 /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(133):
         PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Expr\Assign))                      
         #9 /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):
         PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Stmt\Expression))                  
         #10                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(105):
         PhpParser\NodeTraverser->traverseArray(Array)                                                  
         #11                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(133):
         PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Expr\Closure))                     
         #12                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):
         PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Arg))                              
         #13                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(105):
         PhpParser\NodeTraverser->traverseArray(Array)                                                  
         #14                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(133):
         PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Expr\FuncCall))                    
         #15                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):
         PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Stmt\Expression))                  
         #16                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(105):
         PhpParser\NodeTraverser->traverseArray(Array)                                                  
         #17                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):
         PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Stmt\If_))                         
         #18                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(105):
         PhpParser\NodeTraverser->traverseArray(Array)                                                  
         #19                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):
         PhpParser\NodeTraverser->traverseNode(Object(PhpParser\Node\Stmt\Function_))                   
         #20                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(105):
         PhpParser\NodeTraverser->traverseArray(Array)                                                  
         #21                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):
         PhpParser\NodeTraverser->traverseNode(Object(Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace
         ))                                                                                             
         #22 /var/www/.composer/vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(85):
         PhpParser\NodeTraverser->traverseArray(Array)                                                  
         #23 /var/www/.composer/vendor/rector/rector/src/PhpParser/NodeTraverser/RectorNodeTraverser.php(42):
         PhpParser\NodeTraverser->traverse(Array)                                                       
         #24 /var/www/.composer/vendor/rector/rector/src/Application/FileProcessor.php(61):             
         Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser->traverse(Array)                       
         #25 /var/www/.composer/vendor/rector/rector/src/Application/FileProcessor/PhpFileProcessor.php(126):
         Rector\Core\Application\FileProcessor->refactor(Object(Rector\Core\ValueObject\Application\File),
         Object(Rector\Core\ValueObject\Configuration))                                                 
         #26 /var/www/.composer/vendor/rector/rector/src/Application/FileProcessor/PhpFileProcessor.php(93):
         Rector\Core\Application\FileProcessor\PhpFileProcessor->refactorNodesWithRectors(Object(Rector\Core\Val
         ueObject\Application\File), Object(Rector\Core\ValueObject\Configuration))                     
         #27 /var/www/.composer/vendor/rector/rector/packages/Parallel/WorkerRunner.php(99):            
         Rector\Core\Application\FileProcessor\PhpFileProcessor->process(Object(Rector\Core\ValueObject\Applicat
         ion\File), Object(Rector\Core\ValueObject\Configuration))                                      
         #28                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(9
         7): Rector\Parallel\WorkerRunner->Rector\Parallel\{closure}(Array)                             
         #29 /var/www/.composer/vendor/rector/rector/vendor/clue/ndjson-react/src/Decoder.php(110):     
         RectorPrefix202208\Evenement\EventEmitter->emit('data', Array)                                 
         #30                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(9
         7): RectorPrefix202208\Clue\React\NDJson\Decoder->handleData(Array)                            
         #31 /var/www/.composer/vendor/rector/rector/vendor/react/stream/src/Util.php(62):              
         RectorPrefix202208\Evenement\EventEmitter->emit('data', Array)                                 
         #32                                                                                            
         /var/www/.composer/vendor/rector/rector/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(9
         7):                                                                                            
         RectorPrefix202208\React\Stream\Util::RectorPrefix202208\React\Stream\{closure}('{"action":"main...')
         #33 /var/www/.composer/vendor/rector/rector/vendor/react/stream/src/DuplexResourceStream.php(154):
         RectorPrefix202208\Evenement\EventEmitter->emit('data', Array)                                 
         #34 /var/www/.composer/vendor/rector/rector/vendor/react/event-loop/src/StreamSelectLoop.php(201):
         RectorPrefix202208\React\Stream\DuplexResourceStream->handleData(Resource id #2864)            
         #35 /var/www/.composer/vendor/rector/rector/vendor/react/event-loop/src/StreamSelectLoop.php(173):
         RectorPrefix202208\React\EventLoop\StreamSelectLoop->waitForStreamActivity(NULL)               
         #36 /var/www/.composer/vendor/rector/rector/src/Console/Command/WorkerCommand.php(63):         
         RectorPrefix202208\React\EventLoop\StreamSelectLoop->run()                                     
         #37 /var/www/.composer/vendor/rector/rector/vendor/symfony/console/Command/Command.php(307):   
         Rector\Core\Console\Command\WorkerCommand->execute(Object(RectorPrefix202208\Symfony\Component\Console</>
         Input\ArgvInput), Object(RectorPrefix202208\Symfony\Component\Console\Output\ConsoleOutput))   
         #38 /var/www/.composer/vendor/rector/rector/vendor/symfony/console/Application.php(891):       
         RectorPrefix202208\Symfony\Component\Console\Command\Command->run(Object(RectorPrefix202208\Symfony\Com
         ponent\Console\Input\ArgvInput),                                                               
         Object(RectorPrefix202208\Symfony\Component\Console\Output\ConsoleOutput))                     
         #39 /var/www/.composer/vendor/rector/rector/vendor/symfony/console/Application.php(310):       
         RectorPrefix202208\Symfony\Component\Console\Application->doRunCommand(Object(Rector\Core\Console\Comma
         nd\WorkerCommand), Object(RectorPrefix202208\Symfony\Component\Console\Input\ArgvInput),       
         Object(RectorPrefix202208\Symfony\Component\Console\Output\ConsoleOutput))                     
         #40 /var/www/.composer/vendor/rector/rector/src/Console/ConsoleApplication.php(49):            
         RectorPrefix202208\Symfony\Component\Console\Application->doRun(Object(RectorPrefix202208\Symfony\Compo
         nent\Console\Input\ArgvInput),                                                                 
         Object(RectorPrefix202208\Symfony\Component\Console\Output\ConsoleOutput))                     
         #41 /var/www/.composer/vendor/rector/rector/vendor/symfony/console/Application.php(208):       
         Rector\Core\Console\ConsoleApplication->doRun(Object(RectorPrefix202208\Symfony\Component\Console\Input
         \ArgvInput), Object(RectorPrefix202208\Symfony\Component\Console\Output\ConsoleOutput))        
         #42 /var/www/.composer/vendor/rector/rector/bin/rector.php(128):                               
         RectorPrefix202208\Symfony\Component\Console\Application->run()                                
         #43 /var/www/.composer/vendor/rector/rector/bin/rector(5): require_once('/var/www/.compo...')  
         #44 /var/www/.composer/vendor/bin/rector(120): include('/var/www/.compo...')                   
         #45 {main}". On line: 269  

The php code that generated the error (although the PHP code works fine):

<?php

/**
 * @file
 * FC Search Support module.
 */

/**
 * Implements hook_apachesolr_index_document_build().
 */
function fc_search_support_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
  // Add additional custom fields to the Apache Solr index.
  if ($entity_type == 'node' && $entity->type == 'fc_products') {
    // Add the product preview image to the Solr index as a single value string.
    $preview_image = file_create_url($entity->field_product_image[LANGUAGE_NONE][0]['uri']);
    $document->addField('ss_preview_image', $preview_image);
  }
}

/**
 * Implements hook_apachesolr_query_alter().
 */
function fc_search_support_apachesolr_query_alter($query) {
  // Add the product preview image as an available field that can be returned
  // with Apache Solr searches.
  $query->addParam('fl', 'ss_preview_image');
}

/**
 * Change the "Apply" button text to "Search" on search results pages.
 */
function fc_search_support_preprocess_views_exposed_form(&$vars, $hook) {
  if ($vars['form']['#id'] == 'views-exposed-form-fc-search-page') {
    // Change text on the submit button.
    $vars['form']['submit']['#value'] = t('Search');

    // Rebuild the rendered version.
    unset($vars['form']['submit']['#printed']);
    $vars['button'] = drupal_render($vars['form']['submit']);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * For the search block form.
 */
function fc_search_support_form_search_block_form_alter(&$form, &$form_state) {
  // Modify the default search block form to query the fc_search View for
  // Apache Solr search. This allows the form to appear and function as it did
  // with core search.
  $form['#action'] = '/product-search';
  $form['#method'] = 'get';
  $form['search_block_form']['#name'] = 'keyword';
  unset($form['form_build_id']);
  unset($form['form_token']);
  unset($form['form_id']);
}

/**
 * Implements hook_views_post_execute().
 */
function fc_search_support_views_post_execute(&$view) {
  if ($view->name === 'fc_search') {
    // Re-size preview thumbnails on Search page.
    array_walk($view->result, function (&$result) {
      // The URL in the "src" value is a full URL, but we need one that Drupal
      // understands.
      $uri = preg_replace('[.*\/files\/]', 'public://', $result->ss_preview_image);
      $thumbnail = theme_image_style([
        'path' => $uri,
        'style_name' => 'product_thumbnail',
        'width' => '700',
        'height' => '900',
      ]);
      // Extract the "src" tag provided by theme_image_style().
      $xpath = new DOMXPath(@DOMDocument::loadHTML($thumbnail));
      $src = $xpath->evaluate("string(//img/@src)");
      // The old image can be replaced with the new one.
      $result->ss_preview_image = $src;
    });
  }
}

blue928 avatar Aug 30 '22 00:08 blue928

Thank you for your report!

We'll need an isolated failing demo link from: http://getrector.org/demo, that way we can reproduce the bug.

TomasVotruba avatar Aug 30 '22 07:08 TomasVotruba

Hmm, sorry, I seem to be having trouble with that. Each time I try I get an http 500 error. I tried with the default config from that page. As well, the only thing I change from my local rector.php file after init is to update the ruleset to

        $rectorConfig->sets([
            LevelSetList::UP_TO_PHP_80
        ]);

This also throws a 500 error.

But, the error is on this line: $uri = preg_replace('[.*\/files\/]', 'public://', $result->ss_preview_image);

If I comment it out or rewrite that to be:

$publicDir = 'public://';
$uri = preg_replace('[.*\/files\/]', $publicDir, $result->ss_preview_image);

.. locally it "works" and lets rector continue. It seems that something in the preg_replace function is causing the parser to ignore all the code between '<?php' and 'public' making it think that the code is trying to create a public class method. The updated code, however, also throws a 500 error in the demo. I'll try again in a bit and report back if I can get past the error.

blue928 avatar Aug 30 '22 13:08 blue928

The demo link is back. We had to fix bug with processing huge files.

TomasVotruba avatar Sep 05 '22 11:09 TomasVotruba

This seems like a PHP verison missmatch. Rector has to parse valid PHP code and must run on version that allows parsing it.

TomasVotruba avatar Oct 13 '22 01:10 TomasVotruba