diff --git a/.travis.yml b/.travis.yml index 3c0820a4..ce0edb42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,20 +52,20 @@ before_script: - if [[ "$COVERAGE" = "on" ]]; then ./cc-test-reporter before-build; fi; script: - - if [[ "$STYLECHECK" != "on" && "$COVERAGE" != "on" && "$MULTITEST" != "on" && "$SPLIT" != "on" ]]; then vendor/bin/phpunit --no-coverage; fi; - - if [[ "$COVERAGE" = "on" ]]; then vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml; fi; + - if [[ "$STYLECHECK" != "on" && "$COVERAGE" != "on" && "$MULTITEST" != "on" && "$SPLIT" != "on" ]]; then vendor/bin/phpunit --no-coverage --debug --verbose; fi; + - if [[ "$COVERAGE" = "on" ]]; then vendor/bin/phpunit --no-coverage --debug --verbose; fi; - if [[ "$MULTITEST" = "on" ]]; then vendor/bin/multi-tester --verbose --quiet-install; fi; - if [[ "$STYLECHECK" = "on" ]]; then vendor/bin/phpcs --ignore=*.js src; fi; - if [[ "$SPLIT" = "on" ]]; then composer require phug/split && vendor/bin/split update --git-credentials=$GH_CREDENTIALS; fi; -after_success: - - if [[ "$COVERAGE" = "on" ]]; then bash <(curl -s https://codecov.io/bash); fi; - -after_script: - - if [[ "$COVERAGE" = "on" ]]; then cp coverage.xml clover.xml; fi; - - if [[ "$COVERAGE" = "on" ]]; then ./cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT; fi; - - if [[ "$COVERAGE" = "on" ]]; then composer require codacy/coverage; fi; - - if [[ "$COVERAGE" = "on" ]]; then vendor/bin/codacycoverage clover coverage.xml; fi; +#after_success: +# - if [[ "$COVERAGE" = "on" ]]; then bash <(curl -s https://codecov.io/bash); fi; +# +#after_script: +# - if [[ "$COVERAGE" = "on" ]]; then cp coverage.xml clover.xml; fi; +# - if [[ "$COVERAGE" = "on" ]]; then ./cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT; fi; +# - if [[ "$COVERAGE" = "on" ]]; then composer require codacy/coverage; fi; +# - if [[ "$COVERAGE" = "on" ]]; then vendor/bin/codacycoverage clover coverage.xml; fi; notifications: slack: phug:nzXFnxhU14RWK2EQSDL0u08z diff --git a/src/Phug/Formatter/Formatter/Util/PhpUnwrap.php b/src/Phug/Formatter/Formatter/Util/PhpUnwrap.php index ea285c85..b4568ed3 100644 --- a/src/Phug/Formatter/Formatter/Util/PhpUnwrap.php +++ b/src/Phug/Formatter/Formatter/Util/PhpUnwrap.php @@ -4,39 +4,15 @@ use Phug\Formatter; -class PhpUnwrap +class PhpUnwrap extends PhpUnwrapString { - /** - * @var string - */ - private $code; - public function __construct($element, Formatter $formatter) { - $elements = is_array($element) ? $element : [$element]; - $code = implode('', array_map(function ($child) use ($formatter) { + parent::__construct(implode('', array_map(function ($child) use ($formatter) { return is_string($child) ? $child : $formatter->format($child); - }, $elements)); - $code = preg_match('/^<\?php\s/', $code) - ? mb_substr($code, 6) - : '?>'.$code; - $code = preg_match('/\s\?>$/', $code) && strpos($code, 'code = $code; - } + }, is_array($element) ? $element : [$element]))); - /** - * @return string - */ - public function getCode() - { - return $this->code; - } - - public function __toString() - { - return $this->getCode(); + $this->unwrapStart(); + $this->unwrapEnd(); } } diff --git a/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php b/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php new file mode 100644 index 00000000..5b0929f5 --- /dev/null +++ b/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php @@ -0,0 +1,61 @@ +code = $code; + } + + public static function withoutOpenTag($code) + { + $unwrappedCode = new self($code); + $unwrappedCode->unwrapStart(); + $unwrappedCode->movePhugCodeAfterNamespace(); + + return $unwrappedCode; + } + + public function movePhugCodeAfterNamespace() + { + $this->code = preg_replace( + '/^((?:[ \t]*(?:\\\\Phug\\\\.*\n|\\/\\/.*\n|(?:\?><\?php)?[ \t\n]+)?)*)(namespace (?:.*)\n)/', + '$2$1', + $this->code + ); + } + + public function unwrapStart() + { + $this->code = preg_match('/^<\?php\s/', $this->code) + ? mb_substr($this->code, 6) + : '?>'.$this->code; + } + + public function unwrapEnd() + { + $this->code = preg_match('/\s\?>$/', $this->code) && strpos($this->code, 'code, 0, -3).';' + : $this->code.'code; + } + + public function __toString() + { + return $this->getCode(); + } +} diff --git a/src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php b/src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php index 40e8d498..633218e5 100644 --- a/src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php +++ b/src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php @@ -2,6 +2,7 @@ namespace Phug\Renderer\Adapter; +use Phug\Formatter\Util\PhpUnwrapString; use Phug\Renderer\AbstractAdapter; /** @@ -17,7 +18,7 @@ public function display($__pug_php, array $__pug_parameters) { $this->execute(function () use ($__pug_php, &$__pug_parameters) { extract($__pug_parameters); - eval('?>'.$__pug_php); + eval(PhpUnwrapString::withoutOpenTag($__pug_php)); }, $__pug_parameters); } } diff --git a/tests/Phug/Element/MixinCallElementTest.php b/tests/Phug/Element/MixinCallElementTest.php index c7eb5b25..bc36de6b 100644 --- a/tests/Phug/Element/MixinCallElementTest.php +++ b/tests/Phug/Element/MixinCallElementTest.php @@ -29,6 +29,7 @@ class MixinCallElementTest extends TestCase * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement @@ -83,6 +84,7 @@ public function testMixinCallElement() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement @@ -170,6 +172,7 @@ public function testMixinBlockVariable() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement @@ -220,6 +223,7 @@ public function testDefaultValue() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement @@ -330,6 +334,7 @@ public function testUnknownMixinDebugOff() * @covers \Phug\Formatter::getDestructors * @covers \Phug\Formatter\AbstractFormat::getChildrenIterator * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: */ public function testPhpUnwrap() { diff --git a/tests/Phug/Element/MixinElementTest.php b/tests/Phug/Element/MixinElementTest.php index f779bd11..b46d672d 100644 --- a/tests/Phug/Element/MixinElementTest.php +++ b/tests/Phug/Element/MixinElementTest.php @@ -22,6 +22,7 @@ class MixinElementTest extends TestCase * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement @@ -61,6 +62,7 @@ public function testMixinElement() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement @@ -89,6 +91,7 @@ public function testLazyLoad() * @covers \Phug\Formatter::requireAllMixins * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement @@ -129,6 +132,7 @@ public function testRequireAllMixins() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement @@ -172,6 +176,7 @@ public function testMixinElementReplace() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement diff --git a/tests/Phug/FormatterTest.php b/tests/Phug/FormatterTest.php index f6d3123b..76de1815 100644 --- a/tests/Phug/FormatterTest.php +++ b/tests/Phug/FormatterTest.php @@ -1217,6 +1217,9 @@ public function testDebugError() } catch (\Exception $exception) { /** @var LocatedException $error */ $error = $formatter->getDebugError($exception, $code); + } catch (\Throwable $exception) { + /** @var LocatedException $error */ + $error = $formatter->getDebugError($exception, $code); } }, '?>'.$php); ob_end_clean(); @@ -1355,6 +1358,8 @@ public function testDebugErrorOnRemovedFile() include $file; } catch (\Exception $exception) { $error = $exception; + } catch (\Throwable $exception) { + $error = $exception; } ob_end_clean(); diff --git a/tests/Phug/ProfilerModuleTest.php b/tests/Phug/ProfilerModuleTest.php index 0b92aa0f..aff70dbb 100644 --- a/tests/Phug/ProfilerModuleTest.php +++ b/tests/Phug/ProfilerModuleTest.php @@ -155,10 +155,8 @@ public function testDebugDefaultOptions() */ public function testMemoryLimitOptions() { - if (version_compare(PHP_VERSION, '5.6.0-dev', '>=') && - version_compare(PHP_VERSION, '7.0.0-dev', '<') - ) { - self::markTestSkipped('Skipped due to Travis bug with PHP 5.6 only.'); + if (version_compare(PHP_VERSION, '7.0.0-dev', '<')) { + self::markTestSkipped('Skipped due to Travis bug with PHP 5 only.'); return; } diff --git a/tests/Phug/RendererTest.php b/tests/Phug/RendererTest.php index 03748bc6..53bf071b 100644 --- a/tests/Phug/RendererTest.php +++ b/tests/Phug/RendererTest.php @@ -471,6 +471,8 @@ public function testHandleErrorInString() $renderer->render('div: p=trigger_error("Division by zero")'); } catch (\Exception $error) { $message = $error->getMessage(); + } catch (\Throwable $error) { + $message = $error->getMessage(); } self::assertContains( diff --git a/tests/Phug/Util/PhpUnwrapStringTest.php b/tests/Phug/Util/PhpUnwrapStringTest.php new file mode 100644 index 00000000..9db329db --- /dev/null +++ b/tests/Phug/Util/PhpUnwrapStringTest.php @@ -0,0 +1,23 @@ + + * @covers :: + */ + public function testPhpUnwrapString() + { + self::assertSame('echo "Foo";', (string) PhpUnwrapString::withoutOpenTag('', (string) PhpUnwrapString::withoutOpenTag('')); + self::assertSame('?>
', (string) PhpUnwrapString::withoutOpenTag('
')); + } +} diff --git a/tests/cases/scope.html b/tests/cases/scope.html new file mode 100644 index 00000000..b02def2d --- /dev/null +++ b/tests/cases/scope.html @@ -0,0 +1,2 @@ +test +test2 diff --git a/tests/cases/scope.pug b/tests/cases/scope.pug new file mode 100644 index 00000000..8e644e47 --- /dev/null +++ b/tests/cases/scope.pug @@ -0,0 +1,18 @@ +- + namespace Test; + class Req { + public $text; + public function __construct($n = '') { + $this->text = "test$n\n"; + } + public function suffix($n) { + return new Req($n); + } + } + class Test { + static public function app() { + return new Req(); + } + } += \Test\Test::app()->text += \Test\Test::app()->suffix(2)->text