Skip to content

Commit 11371b3

Browse files
ajthinkingclaude
andcommitted
Keep the console inside PHP 8.1
Constants in traits are PHP 8.2, and this package supports 8.1, so the directive names move from the trait that uses them to a class of their own. CI caught it; my local PHP is 8.4 and the lowest I can install here is 8.2, so nothing on this machine would have. Neither PHPStan with phpVersion 80100 nor php-parser's version-aware parser flags the construct, so there is no local guard to add — the 8.1 job in CI is the check. A scan for the other 8.2-8.4 additions found nothing else. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AjUMv5rFTVJMr6F1J7bx7x
1 parent c52c465 commit 11371b3

5 files changed

Lines changed: 43 additions & 24 deletions

File tree

src/Console/Commands/ClassConstantCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Archetype\Console\EndpointCommand;
66
use Archetype\Console\Support\Code;
7+
use Archetype\Console\Support\Directives;
78
use Archetype\Console\Support\Introspector;
89
use Archetype\LaravelFile as File;
910
use Archetype\Support\Types;
@@ -20,7 +21,7 @@ class ClassConstantCommand extends EndpointCommand
2021

2122
protected function directives(): array
2223
{
23-
return ['add', 'remove', 'clear', 'empty'];
24+
return Directives::WRITING;
2425
}
2526

2627
protected function hasValue(): bool

src/Console/Commands/ModelPropertyCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Archetype\Console\EndpointCommand;
66
use Archetype\Console\Support\Code;
7+
use Archetype\Console\Support\Directives;
78
use Archetype\Console\Support\Introspector;
89
use Archetype\LaravelFile as File;
910
use RuntimeException;
@@ -46,7 +47,7 @@ public function __construct(protected string $property = 'fillable')
4647

4748
protected function directives(): array
4849
{
49-
return ['add', 'remove', 'clear', 'empty'];
50+
return Directives::WRITING;
5051
}
5152

5253
protected function hasValue(): bool

src/Console/Commands/PropertyCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Archetype\Console\EndpointCommand;
66
use Archetype\Console\Support\Code;
7+
use Archetype\Console\Support\Directives;
78
use Archetype\Console\Support\Introspector;
89
use Archetype\LaravelFile as File;
910
use Archetype\Support\Types;
@@ -24,7 +25,7 @@ class PropertyCommand extends EndpointCommand
2425

2526
protected function directives(): array
2627
{
27-
return ['add', 'remove', 'clear', 'empty', 'public', 'protected', 'private', 'static'];
28+
return array_merge(Directives::WRITING, Directives::VISIBILITY, ['static']);
2829
}
2930

3031
protected function hasValue(): bool
@@ -70,7 +71,7 @@ protected function name(): string
7071
*/
7172
protected function withVisibility(File $file, string $name): File
7273
{
73-
foreach (['public', 'protected', 'private'] as $flag) {
74+
foreach (Directives::VISIBILITY as $flag) {
7475
if ($this->option($flag)) {
7576
return $file;
7677
}

src/Console/Concerns/HasDirectiveFlags.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Archetype\Console\Concerns;
44

5+
use Archetype\Console\Support\Directives;
56
use Archetype\LaravelFile as File;
67
use InvalidArgumentException;
78
use Symfony\Component\Console\Input\InputOption;
@@ -16,22 +17,6 @@
1617
*/
1718
trait HasDirectiveFlags
1819
{
19-
/** directive method => flag description */
20-
const DIRECTIVES = [
21-
'add' => 'Add to what is there instead of replacing it',
22-
'remove' => 'Remove it',
23-
'clear' => 'Clear the default value, keeping the declaration',
24-
'empty' => 'Empty it, keeping the declaration',
25-
'full' => 'Answer with the fully qualified name',
26-
'public' => 'Declare it public',
27-
'protected' => 'Declare it protected',
28-
'private' => 'Declare it private',
29-
'static' => 'Declare it static',
30-
];
31-
32-
/** The directives that make an operation a write rather than a read. */
33-
const WRITING_DIRECTIVES = ['add', 'remove', 'clear', 'empty'];
34-
3520
/** Which directives this command's endpoint honours. */
3621
abstract protected function directives(): array;
3722

@@ -54,7 +39,7 @@ protected function withDirectives(File $file): File
5439
/** True when the caller asked a question rather than for a change. */
5540
protected function isRead(): bool
5641
{
57-
foreach (array_intersect($this->directives(), self::WRITING_DIRECTIVES) as $directive) {
42+
foreach (array_intersect($this->directives(), Directives::WRITING) as $directive) {
5843
if ($this->option($directive)) {
5944
return false;
6045
}
@@ -67,7 +52,7 @@ protected function isRead(): bool
6752
protected function guardDirectives(): void
6853
{
6954
$given = array_values(array_filter(
70-
array_intersect($this->directives(), self::WRITING_DIRECTIVES),
55+
array_intersect($this->directives(), Directives::WRITING),
7156
fn ($directive) => $this->option($directive)
7257
));
7358

@@ -78,7 +63,7 @@ protected function guardDirectives(): void
7863
}
7964

8065
$visibility = array_values(array_filter(
81-
['public', 'protected', 'private'],
66+
Directives::VISIBILITY,
8267
fn ($flag) => in_array($flag, $this->directives(), true) && $this->option($flag)
8368
));
8469

@@ -99,7 +84,7 @@ protected function directiveOptions(): array
9984
$directive,
10085
null,
10186
InputOption::VALUE_NONE,
102-
self::DIRECTIVES[$directive]
87+
Directives::ALL[$directive]
10388
);
10489
}
10590

src/Console/Support/Directives.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Archetype\Console\Support;
4+
5+
/**
6+
* The PHP API's directives, as the console names them.
7+
*
8+
* These live on a class rather than on the trait that uses them because
9+
* constants in traits are PHP 8.2, and this package supports 8.1.
10+
*/
11+
class Directives
12+
{
13+
/** directive method => flag description */
14+
const ALL = [
15+
'add' => 'Add to what is there instead of replacing it',
16+
'remove' => 'Remove it',
17+
'clear' => 'Clear the default value, keeping the declaration',
18+
'empty' => 'Empty it, keeping the declaration',
19+
'full' => 'Answer with the fully qualified name',
20+
'public' => 'Declare it public',
21+
'protected' => 'Declare it protected',
22+
'private' => 'Declare it private',
23+
'static' => 'Declare it static',
24+
];
25+
26+
/** The directives that make an operation a write rather than a read. */
27+
const WRITING = ['add', 'remove', 'clear', 'empty'];
28+
29+
/** The directives that choose a visibility. */
30+
const VISIBILITY = ['public', 'protected', 'private'];
31+
}

0 commit comments

Comments
 (0)