|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the humbug/php-scoper package. |
| 7 | + * |
| 8 | + * Copyright (c) 2017 Théo FIDRY <[email protected]>, |
| 9 | + * Pádraic Brady <[email protected]> |
| 10 | + * |
| 11 | + * For the full copyright and license information, please view the LICENSE |
| 12 | + * file that was distributed with this source code. |
| 13 | + */ |
| 14 | + |
| 15 | +namespace Humbug\PhpScoper\PhpParser; |
| 16 | + |
| 17 | +use Humbug\PhpScoper\Scoper\PhpScoper; |
| 18 | +use Humbug\PhpScoper\Whitelist; |
| 19 | +use PhpParser\Error as PhpParserError; |
| 20 | +use PhpParser\Node\Scalar\String_; |
| 21 | +use function substr; |
| 22 | + |
| 23 | +trait StringScoperPrefixer |
| 24 | +{ |
| 25 | + private $scoper; |
| 26 | + private $prefix; |
| 27 | + private $whitelist; |
| 28 | + |
| 29 | + public function __construct(PhpScoper $scoper, string $prefix, Whitelist $whitelist) |
| 30 | + { |
| 31 | + $this->scoper = $scoper; |
| 32 | + $this->prefix = $prefix; |
| 33 | + $this->whitelist = $whitelist; |
| 34 | + } |
| 35 | + |
| 36 | + private function scopeStringValue(String_ $node): void |
| 37 | + { |
| 38 | + try { |
| 39 | + $lastChar = substr($node->value, -1); |
| 40 | + |
| 41 | + $newValue = $this->scoper->scopePhp($node->value, $this->prefix, $this->whitelist); |
| 42 | + |
| 43 | + if ("\n" !== $lastChar) { |
| 44 | + $newValue = substr($newValue, 0, -1); |
| 45 | + } |
| 46 | + |
| 47 | + $node->value = $newValue; |
| 48 | + } catch (PhpParserError $error) { |
| 49 | + // Continue without scoping the heredoc which for some reasons contains invalid PHP code |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments