Skip to content

Commit 6d7e2d2

Browse files
committed
Refactors
1 parent 5cd0d9c commit 6d7e2d2

15 files changed

+84
-133
lines changed

config/blueprint.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,12 @@
121121
| Constructor Property Promotion
122122
|--------------------------------------------------------------------------
123123
|
124-
| PHP 8.0 offers a new feature called "constructor property promotion."
125-
| If you are running PHP >= 8.0, you may enable this feature to tell
126-
| Blueprint to generate constructor signatures and properties for
127-
| your classes automatically if you don't explicitly do so.
124+
| By default, Blueprint generates class properties explicitly. You may enable
125+
| this option to have Blueprint generate code for classes which contain a
126+
| constructor to set properties with "constructor property promotion".
128127
|
129128
*/
130-
'constructor_property_promotion' => false,
129+
'property_promotion' => false,
131130

132131
/*
133132
|--------------------------------------------------------------------------

src/Blueprint.php

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function parse($content, $strip_dashes = true)
3838
$content = preg_replace('/^(\s*)-\s*/m', '\1', $content);
3939
}
4040

41-
$content = $this->transformDuplicatePropertyKeys($content, ['dispatch', 'fire', 'notify', 'send']);
41+
$content = $this->transformDuplicatePropertyKeys($content);
4242

4343
$content = preg_replace_callback(
4444
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi',
@@ -139,43 +139,58 @@ protected function shouldGenerate(array $types, array $only, array $skip): bool
139139
return true;
140140
}
141141

142-
private function transformDuplicatePropertyKeys(string $content, array $properties): string
142+
private function transformDuplicatePropertyKeys(string $content): string
143143
{
144-
$contentArray = explode("\n", $content);
144+
preg_match('/^controllers:$/m', $content, $matches, PREG_OFFSET_CAPTURE);
145145

146-
foreach ($properties as $property) {
147-
if (!str_contains($content, $property)) {
148-
continue;
149-
}
146+
if (empty($matches)) {
147+
return $content;
148+
}
150149

151-
preg_match_all(
152-
sprintf('/[^\S\r\n]*%s: .*/', $property),
153-
$content,
154-
$lines,
155-
PREG_OFFSET_CAPTURE
156-
);
150+
$offset = $matches[0][1];
151+
$lines = explode("\n", substr($content, $offset));
157152

158-
if (count($lines) === 0) {
159-
return $content;
153+
$methods = [];
154+
$statements = [];
155+
foreach ($lines as $index => $line) {
156+
$method = preg_match('/^( {2}| {4}|\t){2}\w+:$/', $line);
157+
if ($method) {
158+
$methods[] = $statements ?? [];
159+
$statements = [];
160+
161+
continue;
160162
}
161163

162-
if (count($lines[0]) <= 1) {
163-
return $content;
164+
preg_match('/^( {2}| {4}|\t){3}(dispatch|fire|notify|send):\s/', $line, $matches);
165+
if (empty($matches)) {
166+
continue;
164167
}
165168

166-
foreach ($lines[0] as $line) {
167-
$lineNumber = count(explode("\n", mb_substr($content, 0, $line[1])));
169+
$statements[$index] = $matches[2];
170+
}
168171

169-
$replacement = str_replace(
170-
$property . ':',
171-
sprintf('"%s-%s":', $property, $lineNumber, trim($line[0])),
172-
$line[0]
173-
);
172+
$methods[] = $statements ?? [];
174173

175-
$contentArray[$lineNumber - 1] = $replacement;
176-
}
174+
$multiples = collect($methods)
175+
->filter(fn ($statements) => count(array_unique($statements)) !== count($statements))
176+
->mapWithKeys(fn ($statements) => $statements);
177+
178+
if ($multiples->isEmpty()) {
179+
return $content;
177180
}
178181

179-
return implode("\n", $contentArray);
182+
foreach ($multiples as $line => $statement) {
183+
$lines[$line] = preg_replace(
184+
'/^(\s+)' . $statement . ':/',
185+
'$1' . $statement . '-' . $line . ':',
186+
$lines[$line]
187+
);
188+
}
189+
190+
return substr_replace(
191+
$content,
192+
implode("\n", $lines),
193+
$offset
194+
);
180195
}
181196
}

src/Generators/PestTestGenerator.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ public function output(Tree $tree): array
5656
$this->create($path, $this->populateStub($stub, $controller));
5757
}
5858

59-
if (count($this->output) > 0) {
60-
$this->create('tests/Pest.php', $this->filesystem->stub('pest.pest.stub'));
61-
}
62-
6359
return $this->output;
6460
}
6561

src/Generators/StatementGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function populateConstructor(string $type, $statement): string
2323

2424
protected function buildProperties(array $data)
2525
{
26-
if (config('blueprint.constructor_property_promotion')) {
26+
if (config('blueprint.property_promotion')) {
2727
return '';
2828
}
2929

@@ -42,7 +42,7 @@ function ($output, $property) {
4242

4343
protected function buildAssignments(array $data)
4444
{
45-
if (config('blueprint.constructor_property_promotion')) {
45+
if (config('blueprint.property_promotion')) {
4646
return '//';
4747
}
4848

@@ -61,7 +61,7 @@ function ($output, $property) {
6161

6262
protected function buildParameters(array $data)
6363
{
64-
if (config('blueprint.constructor_property_promotion')) {
64+
if (config('blueprint.property_promotion')) {
6565
$parameters = array_map(
6666
fn ($parameter) => 'public $' . $parameter,
6767
$data

src/Models/Column.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ public function isUnsigned()
6464
return in_array('unsigned', $this->modifiers);
6565
}
6666

67-
public static function columnName($qualifiedName)
67+
public static function columnName($reference)
6868
{
69-
if (Str::contains($qualifiedName, '.')) {
70-
return Str::after($qualifiedName, '.');
71-
}
72-
73-
return $qualifiedName;
69+
return Str::after($reference, '.');
7470
}
7571
}

src/Models/Statements/FireStatement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public function isNamedEvent(): bool
3737

3838
public function output()
3939
{
40+
$template = '%s::dispatch(%s);';
41+
4042
if ($this->isNamedEvent()) {
4143
if ($this->data()) {
4244
$template = "event('%s', [%s]);";
4345
} else {
4446
$template = "event('%s');";
4547
}
46-
} else {
47-
$template = '%s::dispatch(%s);';
4848
}
4949

5050
return sprintf(

src/Tree.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Tree
88
{
99
private $tree;
1010

11+
private $models = [];
12+
1113
public function __construct(array $tree)
1214
{
1315
$this->tree = $tree;
@@ -40,7 +42,7 @@ public function seeders()
4042
return $this->tree['seeders'];
4143
}
4244

43-
public function modelForContext(string $context, bool $throwWhenMissing = false)
45+
public function modelForContext(string $context, bool $throw = false)
4446
{
4547
if (isset($this->models[Str::studly($context)])) {
4648
return $this->models[Str::studly($context)];
@@ -50,20 +52,20 @@ public function modelForContext(string $context, bool $throwWhenMissing = false)
5052
return $this->models[Str::studly(Str::plural($context))];
5153
}
5254

53-
$matches = array_filter(array_keys($this->models), fn ($key) => Str::endsWith(Str::afterLast(Str::afterLast($key, '\\'), '/'), [Str::studly($context), Str::studly(Str::plural($context))]));
55+
$matches = array_filter(
56+
array_keys($this->models),
57+
fn ($key) => Str::endsWith(Str::afterLast(Str::afterLast($key, '\\'), '/'), [Str::studly($context), Str::studly(Str::plural($context))]
58+
));
5459

55-
if (count($matches) === 1) {
56-
return $this->models[current($matches)];
57-
}
60+
if (count($matches) !== 1) {
61+
if ($throw) {
62+
throw new \InvalidArgumentException(sprintf('The model class [%s] could not be found.', $this->fqcnForContext($context)));
63+
}
5864

59-
if ($throwWhenMissing) {
60-
throw new \InvalidArgumentException(
61-
sprintf(
62-
'The [%s] model class could not be found or autoloaded. Please ensure that the model class name is correctly spelled, adheres to the appropriate namespace, and that the file containing the class is properly located within the "app/Models" directory or another relevant directory as configured.',
63-
$this->fqcnForContext($context),
64-
)
65-
);
65+
return null;
6666
}
67+
68+
return $this->models[current($matches)];
6769
}
6870

6971
public function fqcnForContext(string $context)

stubs/pest.pest.stub

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/Feature/BlueprintTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,22 @@ public function it_parses_yaml_with_multiple_dispatch_fire_notify_send_keys(): v
305305
'controllers' => [
306306
'Post' => [
307307
'store' => [
308+
'dispatch-3' => 'SyncMedia with:post',
308309
'dispatch-4' => 'SyncMedia with:post',
309310
'dispatch-5' => 'SyncMedia with:post',
310-
'dispatch-6' => 'SyncMedia with:post',
311+
'fire-6' => 'NewPost with:post',
311312
'fire-7' => 'NewPost with:post',
312313
'fire-8' => 'NewPost with:post',
313-
'fire-9' => 'NewPost with:post',
314314
],
315315
],
316316
'User' => [
317317
'store' => [
318+
'notify-11' => 'post.author ReviewPost with:post',
318319
'notify-12' => 'post.author ReviewPost with:post',
319320
'notify-13' => 'post.author ReviewPost with:post',
320-
'notify-14' => 'post.author ReviewPost with:post',
321+
'send-14' => 'ReviewNotification to:post.author with:post',
321322
'send-15' => 'ReviewNotification to:post.author with:post',
322323
'send-16' => 'ReviewNotification to:post.author with:post',
323-
'send-17' => 'ReviewNotification to:post.author with:post',
324324
],
325325
],
326326
],

tests/Feature/Generators/PestTestGeneratorTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ public function output_generates_test_for_controller_tree_l8($definition, $path,
7474
$tokens = $this->blueprint->parse($this->fixture($definition));
7575
$tree = $this->blueprint->analyze($tokens);
7676

77-
// dump([$definition, $path, $test]);
78-
7977
$this->subject->output($tree);
80-
81-
// $this->assertEquals(['created' => [...array_keys($paths), 'tests/Pest.php']], $this->subject->output($tree));
8278
}
8379

8480
#[Test]
@@ -112,7 +108,7 @@ public function output_works_for_pascal_case_definition_l8(): void
112108
$tokens = $this->blueprint->parse($this->fixture('drafts/pascal-case.yaml'));
113109
$tree = $this->blueprint->analyze($tokens);
114110

115-
$this->assertEquals(['created' => [$certificateControllerTest, $certificateTypeControllerTest, 'tests/Pest.php']], $this->subject->output($tree));
111+
$this->assertEquals(['created' => [$certificateControllerTest, $certificateTypeControllerTest]], $this->subject->output($tree));
116112
}
117113

118114
#[Test]
@@ -145,7 +141,7 @@ public function output_generates_test_for_controller_tree_using_cached_model_l8(
145141
];
146142
$tree = $this->blueprint->analyze($tokens);
147143

148-
$this->assertEquals(['created' => ['tests/Feature/Http/Controllers/UserControllerTest.php', 'tests/Pest.php']], $this->subject->output($tree));
144+
$this->assertEquals(['created' => ['tests/Feature/Http/Controllers/UserControllerTest.php']], $this->subject->output($tree));
149145
}
150146

151147
#[Test]
@@ -179,7 +175,7 @@ public function output_generates_tests_with_models_with_custom_namespace_correct
179175
$tokens = $this->blueprint->parse($this->fixture($definition));
180176
$tree = $this->blueprint->analyze($tokens);
181177

182-
$this->assertEquals(['created' => [$path, 'tests/Pest.php']], $this->subject->output($tree));
178+
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
183179
}
184180

185181
#[Test]
@@ -214,7 +210,7 @@ public function output_generates_tests_with_pluralized_route_names(): void
214210
$tokens = $this->blueprint->parse($this->fixture($definition));
215211
$tree = $this->blueprint->analyze($tokens);
216212

217-
$this->assertEquals(['created' => [$path, 'tests/Pest.php']], $this->subject->output($tree));
213+
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
218214
}
219215

220216
public static function controllerTreeDataProvider(): array

0 commit comments

Comments
 (0)