Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 8 | class Compiler |
||
| 9 | { |
||
| 10 | /** @var \Spatie\BladeX\BladeX */ |
||
| 11 | protected $bladeX; |
||
| 12 | |||
| 13 | public function __construct(BladeX $bladeX) |
||
| 14 | { |
||
| 15 | return $this->bladeX = $bladeX; |
||
|
|
|||
| 16 | } |
||
| 17 | |||
| 18 | public function compile(string $viewContents): string |
||
| 19 | { |
||
| 20 | return array_reduce( |
||
| 21 | $this->bladeX->registeredComponents(), |
||
| 22 | [$this, 'parseComponentHtml'], |
||
| 23 | $viewContents |
||
| 24 | ); |
||
| 25 | } |
||
| 26 | |||
| 27 | protected function parseComponentHtml(string $viewContents, Component $component) |
||
| 28 | { |
||
| 29 | $viewContents = $this->parseSlots($viewContents); |
||
| 30 | |||
| 31 | $viewContents = $this->parseSelfClosingTags($viewContents, $component); |
||
| 32 | |||
| 33 | $viewContents = $this->parseOpeningTags($viewContents, $component); |
||
| 34 | |||
| 35 | $viewContents = $this->parseClosingTags($viewContents, $component); |
||
| 36 | |||
| 37 | return $viewContents; |
||
| 38 | } |
||
| 39 | |||
| 40 | View Code Duplication | protected function parseSelfClosingTags(string $viewContents, Component $component): string |
|
| 41 | { |
||
| 42 | $pattern = "/<\s*{$component->getTag()}\s*(?<attributes>(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]+\\\"|\'[^\']+\'|[^\'\\\"=<>]+))?)*\s*)\/>/"; |
||
| 43 | |||
| 44 | return preg_replace_callback($pattern, function (array $matches) use ($component) { |
||
| 45 | $attributes = $this->getAttributesFromAttributeString($matches['attributes']); |
||
| 46 | |||
| 47 | return $this->componentString($component, $attributes); |
||
| 48 | }, $viewContents); |
||
| 49 | } |
||
| 50 | |||
| 51 | View Code Duplication | protected function parseOpeningTags(string $viewContents, Component $component): string |
|
| 52 | { |
||
| 53 | $pattern = "/<\s*{$component->getTag()}(?<attributes>(?:\s+[\w\-:]+(=(?:\\\"[^\\\"]*\\\"|\'[^\']*\'|[^\'\\\"=<>]+))?)*\s*)(?<![\/=\-])>/"; |
||
| 54 | |||
| 55 | return preg_replace_callback($pattern, function (array $matches) use ($component) { |
||
| 56 | $attributes = $this->getAttributesFromAttributeString($matches['attributes']); |
||
| 57 | |||
| 58 | return $this->componentStartString($component, $attributes); |
||
| 59 | }, $viewContents); |
||
| 60 | } |
||
| 61 | |||
| 62 | protected function parseClosingTags(string $viewContents, Component $component): string |
||
| 63 | { |
||
| 64 | $pattern = "/<\/\s*{$component->getTag()}\s*>/"; |
||
| 65 | |||
| 66 | return preg_replace($pattern, $this->componentEndString($component), $viewContents); |
||
| 67 | } |
||
| 68 | |||
| 69 | protected function componentString(Component $component, array $attributes = []): string |
||
| 70 | { |
||
| 71 | return $this->componentStartString($component, $attributes).$this->componentEndString($component); |
||
| 72 | } |
||
| 73 | |||
| 74 | protected function componentStartString(Component $component, array $attributes = []): string |
||
| 75 | { |
||
| 76 | $attributesString = $this->attributesToString($attributes); |
||
| 77 | |||
| 78 | $componentAttributeString = "[{$attributesString}]"; |
||
| 79 | |||
| 80 | if ($component->view === 'bladex::context') { |
||
| 81 | return " @php(app(Spatie\BladeX\ContextStack::class)->push({$componentAttributeString})) "; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($component->viewModel) { |
||
| 85 | $componentAttributeString = " |
||
| 86 | array_merge( |
||
| 87 | app(Spatie\BladeX\ContextStack::class)->read(), |
||
| 88 | {$componentAttributeString}, |
||
| 89 | app( |
||
| 90 | '{$component->viewModel}', |
||
| 91 | array_merge( |
||
| 92 | app(Spatie\BladeX\ContextStack::class)->read(), |
||
| 93 | {$componentAttributeString} |
||
| 94 | ) |
||
| 95 | )->toArray() |
||
| 96 | )"; |
||
| 97 | } |
||
| 98 | |||
| 99 | return " @component( |
||
| 100 | '{$component->view}', |
||
| 101 | array_merge(app(Spatie\BladeX\ContextStack::class)->read(), |
||
| 102 | {$componentAttributeString}) |
||
| 103 | ) "; |
||
| 104 | } |
||
| 105 | |||
| 106 | protected function componentEndString(Component $component): string |
||
| 107 | { |
||
| 108 | if ($component->view === 'bladex::context') { |
||
| 109 | return "@php(app(Spatie\BladeX\ContextStack::class)->pop())"; |
||
| 110 | } |
||
| 111 | |||
| 112 | return ' @endcomponent '; |
||
| 113 | } |
||
| 114 | |||
| 115 | protected function getAttributesFromAttributeString(string $attributeString): array |
||
| 116 | { |
||
| 117 | $attributeString = $this->parseBindAttributes($attributeString); |
||
| 118 | |||
| 119 | $pattern = '/(?<attribute>[\w:-]+)(=(?<value>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+)))?/'; |
||
| 120 | |||
| 121 | if (! preg_match_all($pattern, $attributeString, $matches, PREG_SET_ORDER)) { |
||
| 122 | return []; |
||
| 123 | } |
||
| 124 | |||
| 125 | return collect($matches)->mapWithKeys(function ($match) { |
||
| 126 | $attribute = Str::camel($match['attribute']); |
||
| 127 | $value = $match['value'] ?? null; |
||
| 128 | |||
| 129 | if (is_null($value)) { |
||
| 130 | $value = 'true'; |
||
| 131 | $attribute = Str::start($attribute, 'bind:'); |
||
| 132 | } |
||
| 133 | |||
| 134 | $value = $this->stripQuotes($value); |
||
| 135 | |||
| 136 | if ($this->containsEchoes($value)) { |
||
| 137 | $attribute = Str::start($attribute, 'bind:'); |
||
| 138 | $value = $this->compileEchoes($value); |
||
| 139 | } |
||
| 140 | |||
| 141 | if (Str::startsWith($attribute, 'bind:')) { |
||
| 142 | $attribute = Str::after($attribute, 'bind:'); |
||
| 143 | } else { |
||
| 144 | $value = str_replace("'", "\\'", $value); |
||
| 145 | $value = "'{$value}'"; |
||
| 146 | } |
||
| 147 | |||
| 148 | return [$attribute => $value]; |
||
| 149 | })->toArray(); |
||
| 150 | } |
||
| 151 | |||
| 152 | protected function parseSlots(string $viewContents): string |
||
| 166 | |||
| 167 | protected function parseBindAttributes(string $attributeString): string |
||
| 168 | { |
||
| 169 | return preg_replace("/\s*:([\w-]+)=/m", ' bind:$1=', $attributeString); |
||
| 170 | } |
||
| 171 | |||
| 172 | protected function attributesToString(array $attributes): string |
||
| 173 | { |
||
| 174 | return collect($attributes) |
||
| 175 | ->map(function (string $value, string $attribute) { |
||
| 176 | return "'{$attribute}' => {$value}"; |
||
| 177 | }) |
||
| 180 | |||
| 181 | protected function stripQuotes(string $string): string |
||
| 189 | |||
| 190 | protected function containsEchoes(string $value): bool |
||
| 194 | |||
| 195 | protected function compileEchoes(string $value): string |
||
| 218 | } |
||
| 219 |