rcrowe / TwigBridge

Give the power of Twig to Laravel
MIT License
895 stars 165 forks source link

Linter issue when facing an error with Twig 2.0+ #364

Closed kblais closed 4 years ago

kblais commented 5 years ago

Hi, I'm facing an issue with the twig:lint command, that completely fails when facing an issue on my project (PHP 7.3, Laravel 5.8, Twigbridge 0.11.0, Twig 2.4.8) :


   ErrorException  : explode() expects parameter 2 to be string, object given

  at /project/path/vendor/rcrowe/twigbridge/src/Command/Lint.php:342
    338|          * @return array
    339|          */
    340|         protected function getContext($template, $line, $context = 3)
    341|         {
  > 342|         $lines    = explode("\n", $template);
    343|         $position = max(0, $line - $context);
    344|         $max      = min(count($lines), $line - 1 + $context);
    345| 
    346|         $result = [];

  Exception trace:

  1   explode("
", Object(Twig_Source))
      /project/path/vendor/rcrowe/twigbridge/src/Command/Lint.php:342

  2   TwigBridge\Command\Lint::getContext(Object(Twig_Source))
      /project/path/vendor/rcrowe/twigbridge/src/Command/Lint.php:303

  Please use the argument -v to see more details.

And this is completely logical : we pass a Twig\Source object (created by \TwigBridge\Twig\Loader::getSourceContext()) to the validate() method, so, in case of an exception here, the template key contains a Twig\Source object, and not a string.

This pull request fetches the code from the Twig\Source object instead of considering $template is a string (it can't since Twig > 2.0).

coveralls commented 4 years ago

Coverage Status

Coverage remained the same at 54.418% when pulling 858584ed7af2b07ea247ad381ca0a0f56b777729 on kblais:fix-linter-object-given-issue into b50247faf2f570eb2b2d0d3c98b4461f1ef60b2d on rcrowe:master.

kblais commented 4 years ago

@barryvdh do you have a feedback about this ? I'vebeen using my fork for a long time to avoid this, but it would be nice to have this fix merged ;)