Completed
Push — master ( 40534e...1d85b8 )
by Freek
01:16 queued 12s
created

Compiler::getAttributesFromAttributeString()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 8.2505
c 0
b 0
f 0
cc 6
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Illuminate\Support\Str;
6
7
class Compiler
8
{
9
    /** @var \Spatie\BladeX\BladeX */
10
    protected $bladeX;
11
12
    public function __construct(BladeX $bladeX)
13
    {
14
        return $this->bladeX = $bladeX;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
15
    }
16
17
    public function compile(string $viewContents): string
18
    {
19
        return array_reduce(
20
            $this->bladeX->registeredComponents(),
21
            [$this, 'parseComponentHtml'],
22
            $viewContents
23
        );
24
    }
25
26
    protected function parseComponentHtml(string $viewContents, Component $component)
27
    {
28
        $viewContents = $this->parseSlots($viewContents);
29
30
        $viewContents = $this->parseSelfClosingTags($viewContents, $component);
31
32
        $viewContents = $this->parseOpeningTags($viewContents, $component);
33
34
        $viewContents = $this->parseClosingTags($viewContents, $component);
35
36
        return $viewContents;
37
    }
38
39 View Code Duplication
    protected function parseSelfClosingTags(string $viewContents, Component $component): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $pattern = "/
42
            <
43
                \s*
44
                {$component->getTag()}
45
                \s*
46
                (?<attributes>
47
                    (?:
48
                        \s+
49
                        [\w\-:.]+
50
                        (
51
                            =
52
                            (?:
53
                                \\\"[^\\\"]+\\\"
54
                                |
55
                                \'[^\']+\'
56
                                |
57
                                [^\'\\\"=<>]+
58
                            )
59
                        )?
60
                    )*
61
                    \s*
62
                )
63
            \/>
64
        /x";
65
66
        return preg_replace_callback($pattern, function (array $matches) use ($component) {
67
            $attributes = $this->getAttributesFromAttributeString($matches['attributes']);
68
69
            return $this->componentString($component, $attributes);
70
        }, $viewContents);
71
    }
72
73 View Code Duplication
    protected function parseOpeningTags(string $viewContents, Component $component): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $pattern = "/
76
            <
77
                \s*
78
                {$component->getTag()}
79
                (?<attributes>
80
                    (?:
81
                        \s+
82
                        [\w\-:.]+
83
                        (
84
                            =
85
                            (?:
86
                                \\\"[^\\\"]*\\\"
87
                                |
88
                                \'[^\']*\'
89
                                |
90
                                [^\'\\\"=<>]+
91
                            )
92
                        )
93
                    ?)*
94
                    \s*
95
                )
96
                (?<![\/=\-])
97
            >
98
        /x";
99
100
        return preg_replace_callback($pattern, function (array $matches) use ($component) {
101
            $attributes = $this->getAttributesFromAttributeString($matches['attributes']);
102
103
            return $this->componentStartString($component, $attributes);
104
        }, $viewContents);
105
    }
106
107
    protected function parseClosingTags(string $viewContents, Component $component): string
108
    {
109
        $pattern = "/<\/\s*{$component->getTag()}\s*>/";
110
111
        return preg_replace($pattern, $this->componentEndString($component), $viewContents);
112
    }
113
114
    protected function componentString(Component $component, array $attributes = []): string
115
    {
116
        return $this->componentStartString($component, $attributes).$this->componentEndString($component);
117
    }
118
119
    protected function componentStartString(Component $component, array $attributes = []): string
120
    {
121
        $attributesString = $this->attributesToString($attributes);
122
123
        $componentAttributeString = "[{$attributesString}]";
124
125
        if ($component->view === 'bladex::context') {
126
            return " @php(app(Spatie\BladeX\ContextStack::class)->push({$componentAttributeString})) ";
127
        }
128
129
        if ($component->viewModel) {
130
            $componentAttributeString = "
131
                array_merge(
132
                    app(Spatie\BladeX\ContextStack::class)->read(),
133
                    {$componentAttributeString},
134
                    app(
135
                        '{$component->viewModel}',
136
                        array_merge(
137
                            app(Spatie\BladeX\ContextStack::class)->read(),
138
                            {$componentAttributeString}
139
                        )
140
                    )->toArray()
141
                )";
142
        }
143
144
        return " @component(
145
           '{$component->view}',
146
           array_merge(app(Spatie\BladeX\ContextStack::class)->read(),
147
           {$componentAttributeString})
148
        ) ";
149
    }
150
151
    protected function componentEndString(Component $component): string
152
    {
153
        if ($component->view === 'bladex::context') {
154
            return "@php(app(Spatie\BladeX\ContextStack::class)->pop())";
155
        }
156
157
        return ' @endcomponent ';
158
    }
159
160
    protected function getAttributesFromAttributeString(string $attributeString): array
161
    {
162
        $attributeString = $this->parseBindAttributes($attributeString);
163
164
        $pattern = '/
165
            (?<attribute>[\w\-:.]+)
166
            (
167
                =
168
                (?<value>
169
                    (
170
                        \"[^\"]+\"
171
                        |
172
                        \\\'[^\\\']+\\\'
173
                        |
174
                        [^\s>]+
175
                    )
176
                )
177
            )?
178
        /x';
179
180
        if (! preg_match_all($pattern, $attributeString, $matches, PREG_SET_ORDER)) {
181
            return [];
182
        }
183
184
        $namespaces = collect();
185
        $attributes = collect($matches)->mapWithKeys(function ($match) use ($namespaces) {
186
            $attribute = Str::camel($match['attribute']);
187
            $value = $match['value'] ?? null;
188
189
            if (is_null($value)) {
190
                $value = 'true';
191
                $attribute = Str::start($attribute, 'bind:');
192
            }
193
194
            $value = $this->stripQuotes($value);
195
196
            if (Str::startsWith($attribute, 'bind:')) {
197
                $attribute = Str::after($attribute, 'bind:');
198
            } else {
199
                $value = str_replace("'", "\\'", $value);
200
                $value = "'{$value}'";
201
202
                if (Str::contains($attribute, ':')) {
203
                    $namespace = Str::before($attribute, ':');
204
                    if (! $namespaces->has($namespace)) {
205
                        $namespaces->put($namespace, collect());
206
                    }
207
208
                    $attribute = Str::after($attribute, ':');
209
                    $namespaces[$namespace]->put($attribute, $value);
210
211
                    return [];
212
                }
213
            }
214
215
            return [$attribute => $value];
216
        });
217
218
        return $attributes->merge($namespaces)->toArray();
219
    }
220
221
    protected function parseSlots(string $viewContents): string
222
    {
223
        $openingPattern = '/<\s*slot\s+name=(?<name>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+))\s*>/';
224
        $viewContents = preg_replace_callback($openingPattern, function ($matches) {
225
            $name = $this->stripQuotes($matches['name']);
226
227
            return " @slot('{$name}') ";
228
        }, $viewContents);
229
230
        $closingPattern = '/<\/\s*slot[^>]*>/';
231
        $viewContents = preg_replace($closingPattern, ' @endslot', $viewContents);
232
233
        return $viewContents;
234
    }
235
236
    /**
237
     * Adds the `bind:` prefix for all bound data attributes.
238
     * E.g. `foo=bar :name=alex` becomes `foo=bar bind:name=alex`.
239
     *
240
     * @param string $attributeString
241
     *
242
     * @return string
243
     */
244
    protected function parseBindAttributes(string $attributeString): string
245
    {
246
        $pattern = "/
247
            (?:^|\s+)   # start of the string or whitespace between attributes
248
            :           # attribute needs to start with a semicolon
249
            ([\w-]+)    # match the actual attribute name
250
            =           # only match attributes that have a value
251
        /xm";
252
253
        return preg_replace($pattern, ' bind:$1=', $attributeString);
254
    }
255
256
    protected function attributesToString(array $attributes): string
257
    {
258
        return collect($attributes)
259
            ->map(function ($value, string $attribute) {
260
                if (is_array($value)) {
261
                    $value = '['.$this->attributesToString($value).']';
262
                }
263
264
                return "'{$attribute}' => {$value}";
265
            })
266
            ->implode(',');
267
    }
268
269
    protected function stripQuotes(string $string): string
270
    {
271
        if (Str::startsWith($string, ['"', '\''])) {
272
            return substr($string, 1, -1);
273
        }
274
275
        return $string;
276
    }
277
}
278