-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfigInitCommand.php
More file actions
174 lines (154 loc) · 6.83 KB
/
Copy pathConfigInitCommand.php
File metadata and controls
174 lines (154 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace TypePHP\Command;
final class ConfigInitCommand implements CommandInterface
{
public function execute(array $args, $outputStream = STDOUT, $errorStream = STDERR): int
{
$c = [CliFormatter::class, 'color'];
$cwd = getcwd();
$targetFile = ($cwd !== false ? $cwd : '.') . '/typephp.php';
fwrite($outputStream, "\n " . $c(' TYPEPHP ', 'badge') . ' ' . $c('Configuration Initializer', 'bold') . "\n\n");
if (file_exists($targetFile)) {
fwrite($outputStream, ' ' . $c('•', 'cyan') . ' Configuration file ' . $c('"typephp.php"', 'bold') . " already exists.\n\n");
return 0;
}
$template = self::getTemplate();
file_put_contents($targetFile, $template);
fwrite($outputStream, ' ' . $c('✓', 'green') . ' Created ' . $c('"typephp.php"', 'bold') . " in project root directory.\n\n");
return 0;
}
private static function getTemplate(): string
{
return <<<'PHP'
<?php
declare(strict_types=1);
return [
/*
|--------------------------------------------------------------------------
| Global Master Switch
|--------------------------------------------------------------------------
| Controls whether TypePHP enforces type checks at runtime.
| Set to false for an emergency kill-switch or zero-overhead benchmarking.
|
| Note on Disabling Approaches:
| - Config Switch ('enabled' => false): TypePHP boots normally, but turns all
| runtime checks into instant no-ops (pass-through mode).
| - Bootstrap Prevention (TYPEPHP_DISABLE=true): To completely prevent TypePHP
| from booting or registering its stream wrapper during Composer autoload,
| set the environment variable TYPEPHP_DISABLE=true or define('TYPEPHP_DISABLE', true)
| before requiring 'vendor/autoload.php'.
*/
'enabled' => true,
/*
|--------------------------------------------------------------------------
| Function Boundary Contracts (@param & @return)
|--------------------------------------------------------------------------
| Controls whether function and method parameter/return contracts are enforced.
| When enabled, all parameter and return types (generics, shapes, scalars)
| are enforced uniformly to maintain type state consistency.
*/
'params' => true,
'returns' => true,
/*
|--------------------------------------------------------------------------
| Magic Annotations (@property & @method)
|--------------------------------------------------------------------------
| Enforces class-level annotations for dynamic properties and magic methods
| routed through __get, __set, __call, and __callStatic.
*/
'magic_properties' => true,
'magic_methods' => true,
/*
|--------------------------------------------------------------------------
| Respect Ignore Docblock Tags
|--------------------------------------------------------------------------
| When true (default), @typephp-ignore and @typephp-ignore-file docblock tags
| skip type-checking on specific methods/files. Set to false in CI/CD or
| audit runs to force type-checking on all ignored methods without deleting
| the docblock tags from source code.
*/
'respect_ignore_tags' => true,
/*
|--------------------------------------------------------------------------
| Enable Caching & Cache Directory
|--------------------------------------------------------------------------
| When enabled, transformed PHP files are cached on disk for speed.
| Set to false to run AST transformations purely in RAM (php://memory).
|
| 'cache_dir' determines where these files are stored. By default (null),
| it uses your system's temp directory. You can change this to a path
| inside your project, e.g., __DIR__ . '/storage/framework/typephp'.
| TypePHP will automatically protect this directory from being re-transformed.
*/
'cache' => true,
'cache_dir' => null,
/*
|--------------------------------------------------------------------------
| Registered Extensions
|--------------------------------------------------------------------------
| Explicitly list third-party extension classes that provide path overrides.
*/
'extensions' => [
// \Acme\Domain\TypePHPExtension::class,
],
/*
|--------------------------------------------------------------------------
| Inline Variable Validation (@var $x = ...)
|--------------------------------------------------------------------------
| Fine-grained control over which type categories are enforced on local
| variable assignments with inline @var Type $var docblocks.
|
| Supported options:
| - 'properties': Validates class property assignments (e.g. $this->id = 1).
| - 'generics' : Prebinds generic template instances (e.g. Collection<Dog>).
| - 'callables' : Wraps inline callbacks (e.g. callable(int): string).
| - 'scalars' : Enforces scalar constraints (e.g. positive-int, non-empty-string).
| - 'arrays' : Enforces array shapes, lists, & typed arrays (e.g. array{id: int}, int[]).
| - 'objects' : Enforces class instance checks (e.g. @var User $user).
*/
'inline_vars' => [
'properties' => true,
'generics' => true,
'callables' => true,
'scalars' => true,
'arrays' => true,
'objects' => true,
],
/*
|--------------------------------------------------------------------------
| Included Paths & Whitelisting
|--------------------------------------------------------------------------
| Globs or specific file paths that should be intercepted and type-checked.
|
| Pattern Specificity:
| More specific patterns take precedence over broader rules.
| You can specify directory globs (e.g. 'src/**'), single vendor packages
| (e.g. 'vendor/my-org/my-package/**'), or single specific files
| (e.g. 'vendor/monolog/monolog/src/Monolog/Logger.php').
*/
'include' => [
'src/**',
'app/**',
'tests/**',
// 'vendor/my-org/my-package/**', // Whitelist a vendor package
],
/*
|--------------------------------------------------------------------------
| Excluded Paths & Single-File Blacklisting
|--------------------------------------------------------------------------
| Globs or specific file paths that should be ignored by the type checker.
| You can exclude entire directories (e.g. 'vendor/**') or blacklist
| single legacy files inside included directories (e.g. 'src/Legacy/File.php').
*/
'exclude' => [
'vendor/**',
'storage/**',
'var/**',
'cache/**',
// 'src/Legacy/UnsafeFile.php', // Blacklist a single specific file
],
];
PHP;
}
}