diff --git a/src/Generators/ViewGenerator.php b/src/Generators/ViewGenerator.php index 56cc0ed..13fe256 100644 --- a/src/Generators/ViewGenerator.php +++ b/src/Generators/ViewGenerator.php @@ -10,21 +10,16 @@ class ViewGenerator implements Generator { - /** - * @var \Illuminate\Contracts\Filesystem\Filesystem - */ - private $files; - const tall_forms_actions = [ + private ?\Illuminate\Contracts\Filesystem\Filesystem $files; + + private const tall_forms_actions = [ 'create', 'store', 'edit', 'update', 'destroy' ]; - /** - * @var bool - */ - private $is_tall_form = false; + private bool $is_tall_form = false; - private $model = ''; + private ?string $model = ''; public function __construct($files) { @@ -41,7 +36,7 @@ public function output(Tree $tree): array foreach ($tree->controllers() as $controller) { foreach ($controller->methods() as $method => $statements) { //start tall-forms - if (in_array($method, self::tall_forms_actions)) { + if (in_array($method, self::tall_forms_actions, false)) { $this->is_tall_form = true; $this->model = \Str::lower($controller->name()); } else { @@ -80,7 +75,7 @@ public function types(): array return ['controllers', 'views']; } - protected function getPath(string $view) + protected function getPath(string $view): string { return 'resources/views/' . str_replace('.', '/', $view) . '.blade.php'; } @@ -88,8 +83,7 @@ protected function getPath(string $view) protected function populateStub(string $stub, RenderStatement $renderStatement): string { if ($this->is_tall_form) { - $stub = str_replace('{{--', null, $stub); - $stub = str_replace('--}}', null, $stub); + $stub = str_replace(['{{--', '--}}'], [null, null], $stub); $template = 'model . '-form :' . $this->model . '="$' . $this->model . '"/>'; $stub = str_replace('{{ view }} template', $template, $stub); } else { diff --git a/src/HasStubPath.php b/src/HasStubPath.php index f232f66..c0d6739 100644 --- a/src/HasStubPath.php +++ b/src/HasStubPath.php @@ -6,8 +6,6 @@ trait HasStubPath { /** * Returns the stub path for this package. - * - * @return string */ protected function stubPath(): string { diff --git a/src/TallBlueprintAddonServiceProvider.php b/src/TallBlueprintAddonServiceProvider.php index 330934c..8482376 100644 --- a/src/TallBlueprintAddonServiceProvider.php +++ b/src/TallBlueprintAddonServiceProvider.php @@ -19,7 +19,7 @@ class TallBlueprintAddonServiceProvider extends ServiceProvider implements Defer /** * Bootstrap the application services. */ - public function boot() + public function boot(): void { if ($this->app->runningInConsole()) { $this->publishes([ @@ -31,7 +31,7 @@ public function boot() /** * Register the application services. */ - public function register() + public function register(): void { $this->mergeConfigFrom( dirname(__DIR__) . '/config/tall_forms_blueprint.php', @@ -65,10 +65,8 @@ public function register() /** * Get the services provided by the provider. - * - * @return array */ - public function provides() + public function provides(): array { return [ 'command.blueprint.build', diff --git a/src/TallBlueprintGenerator.php b/src/TallBlueprintGenerator.php index c17481f..b3d9e2d 100644 --- a/src/TallBlueprintGenerator.php +++ b/src/TallBlueprintGenerator.php @@ -16,14 +16,11 @@ class TallBlueprintGenerator implements Generator { use HasStubPath, HasSharedGeneratorFunctions; - /** @var \Illuminate\Contracts\Filesystem\Filesystem */ - protected $files; + protected ?\Illuminate\Contracts\Filesystem\Filesystem $files; - /** @var array */ - private $imports = []; + private array $imports = []; - /** @var array */ - private $tasks = []; + private array $tasks = []; public function __construct($files) { @@ -71,12 +68,12 @@ protected function populateStub(string $stub, Model $model): string ->thenReturn(); - $stub = $this->sharedStrReplace($stub, $model->name(), $model->fullyQualifiedClassName()); + $stub = (string)$this->sharedStrReplace($stub, $model->name(), $model->fullyQualifiedClassName()); $stub = str_replace('// fields...', $data['fields'], $stub); + $this->imports = array_unique(array_merge($this->imports, $data['imports'])); - $stub = str_replace('use Tanthammar\TallForms\TallFormComponent;', implode(PHP_EOL, $data['imports']), $stub); - return $stub; + return str_replace('use Tanthammar\TallForms\TallFormComponent;', implode(PHP_EOL, $data['imports']), $stub); } diff --git a/src/TallMethodsBlueprintGenerator.php b/src/TallMethodsBlueprintGenerator.php index 161a576..9573238 100644 --- a/src/TallMethodsBlueprintGenerator.php +++ b/src/TallMethodsBlueprintGenerator.php @@ -14,8 +14,7 @@ class TallMethodsBlueprintGenerator implements Generator { use HasStubPath, HasSharedGeneratorFunctions; - /** @var \Illuminate\Contracts\Filesystem\Filesystem */ - protected $files; + protected ?\Illuminate\Contracts\Filesystem\Filesystem $files; public function __construct($files) { @@ -51,35 +50,37 @@ protected function outputPath($name): string } - protected function populateStub(string $stub, \Blueprint\Models\Controller $controller) + protected function populateStub(string $stub, \Blueprint\Models\Controller $controller): array|string { $data = []; foreach ($controller->methods() as $name => $statements) { data_set($data, 'name', $controller->name()); //switch action name - if ($name == 'store') { + if ($name === 'store') { data_set($data, 'action', 'create'); $data = (new onCreate($statements, $data))->handle(); } - if ($name == 'update') { + if ($name === 'update') { data_set($data, 'action', 'update'); $data = (new onUpdate($statements, $data))->handle(); } - if ($name == 'destroy') { + if ($name === 'destroy') { data_set($data, 'action', 'delete'); $data = (new onDelete($statements, $data))->handle(); } } - $stub = str_replace('// create...', data_get($data, 'create'), $stub); - $stub = str_replace('// update...', data_get($data, 'update'), $stub); - $stub = str_replace('// delete...', data_get($data, 'delete'), $stub); - $stub = $this->sharedStrReplace($stub, $controller->name(), $controller->fullyQualifiedClassName()); + $stub = str_replace( + ['// create...', '// update...', '// delete...'], + [data_get($data, 'create'), data_get($data, 'update'), data_get($data, 'delete')], + $stub); + + $stub = (string)$this->sharedStrReplace($stub, $controller->name(), $controller->fullyQualifiedClassName()); + $imports = array_unique(data_get($data, 'imports', [])); - $stub = str_replace('use Controllers;', implode(PHP_EOL, $imports), $stub); - return $stub; + return str_replace('use Controllers;', implode(PHP_EOL, $imports), $stub); } diff --git a/src/Tasks/AddIdentifierField.php b/src/Tasks/AddIdentifierField.php index 12410b5..debb80c 100644 --- a/src/Tasks/AddIdentifierField.php +++ b/src/Tasks/AddIdentifierField.php @@ -12,7 +12,7 @@ class AddIdentifierField implements Task { use InteractWithRelationships; - const INDENT = ' '; + protected const INDENT = ' '; public function handle($data, Closure $next): array { diff --git a/src/Tasks/AddRegularFields.php b/src/Tasks/AddRegularFields.php index 214b684..aab74cc 100644 --- a/src/Tasks/AddRegularFields.php +++ b/src/Tasks/AddRegularFields.php @@ -10,9 +10,9 @@ class AddRegularFields implements Task { - const INDENT = ' '; - const INDENT_PLUS = ' '; - const LICENCE_TYPE_FIELDS = [ + protected const INDENT = ' '; + protected const INDENT_PLUS = ' '; + protected const LICENCE_TYPE_FIELDS = [ 'Input' => 'Input', 'Number' => 'Input', 'DatePicker' => 'Input', @@ -74,7 +74,9 @@ public function handle($data, Closure $next): array 'unsignedtinyinteger', ] )) { - if (!$sponsor) $field .= PHP_EOL . self::INDENT_PLUS . "->type('number')"; + if (!$sponsor) { + $field .= PHP_EOL . self::INDENT_PLUS . "->type('number')"; + } $field .= PHP_EOL . self::INDENT_PLUS . '->step(1)->min(1)'; } @@ -85,7 +87,9 @@ public function handle($data, Closure $next): array 'unsigneddecimal', ] )) { - if (!$sponsor) $field .= PHP_EOL . self::INDENT_PLUS . "->type('number')"; + if (!$sponsor) { + $field .= PHP_EOL . self::INDENT_PLUS . "->type('number')"; + } $field .= PHP_EOL . self::INDENT_PLUS . '->step(0.10)->min(0.10)'; } @@ -130,7 +134,7 @@ private function addRules(Column $column, string $tableName): string return PHP_EOL . self::INDENT_PLUS . '->rules([' . trim(implode(',', $rules)) . '])'; } - private function fieldType(string $dataType) + private function fieldType(string $dataType): string { static $fieldTypes = [ 'id' => 'Number', diff --git a/src/Tasks/AddRelationshipFields.php b/src/Tasks/AddRelationshipFields.php index 06a6277..094d691 100644 --- a/src/Tasks/AddRelationshipFields.php +++ b/src/Tasks/AddRelationshipFields.php @@ -12,7 +12,7 @@ class AddRelationshipFields implements Task { use InteractWithRelationships; - const INDENT = ' '; + protected const INDENT = ' '; public function handle(array $data, Closure $next): array { @@ -56,17 +56,11 @@ public function handle(array $data, Closure $next): array $fields .= ')'; + $fields .= match ($fieldType) { + 'Select', 'MultiSelect' => '->options(/* TODO pass Array|Collection $' . $label . 'Options */)', + 'KeyVal', 'Repeater' => "->fields([/* TODO add {$label} fields */])", + }; - switch ($fieldType) { - case 'Select': - case 'MultiSelect': - $fields .= '->options(/* TODO pass Array|Collection $'.$label.'Options */)'; - break; - case 'KeyVal': - case 'Repeater': - $fields .= "->fields([/* TODO add {$label} fields */])"; - break; - } $fields .= "->relation(/* TODO create a save{$label}() event hook */)"; if ($this->isNullable($reference, $model)) { @@ -85,7 +79,7 @@ public function handle(array $data, Closure $next): array return $next($data); } - private function buildMethodName(string $name, string $type) + private function buildMethodName(string $name, string $type): string { static $pluralRelations = [ 'belongstomany', @@ -93,7 +87,7 @@ private function buildMethodName(string $name, string $type) 'morphmany', ]; - return in_array(strtolower($type), $pluralRelations) + return in_array(strtolower($type), $pluralRelations, false) ? Str::plural($name) : $name; } @@ -108,13 +102,13 @@ private function isNullable($relation, Model $model): bool { $relationColumnName = $this->relationshipIdentifiers($model->columns()) ->filter(function ($relationReference, $columnName) use ($relation, $model) { - return in_array($relationReference, Arr::get($model->relationships(), 'belongsTo', [])) + return in_array($relationReference, Arr::get($model->relationships(), 'belongsTo', []), false) && $columnName === $relation; }) ->first(); return ! is_null($relationColumnName) - && in_array('nullable', $model->columns()[$relationColumnName]->modifiers()); + && in_array('nullable', $model->columns()[$relationColumnName]->modifiers(), false); } private function fieldType(string $dataType): string diff --git a/src/Tasks/AddTimestampFields.php b/src/Tasks/AddTimestampFields.php index 3210ec4..0beb817 100644 --- a/src/Tasks/AddTimestampFields.php +++ b/src/Tasks/AddTimestampFields.php @@ -7,7 +7,7 @@ class AddTimestampFields implements Task { - const INDENT = ' '; + protected const INDENT = ' '; public function handle($data, Closure $next): array { diff --git a/src/Tasks/HasSharedGeneratorFunctions.php b/src/Tasks/HasSharedGeneratorFunctions.php index 6dd1181..926578b 100644 --- a/src/Tasks/HasSharedGeneratorFunctions.php +++ b/src/Tasks/HasSharedGeneratorFunctions.php @@ -8,13 +8,12 @@ trait HasSharedGeneratorFunctions { - protected function sharedStrReplace(string $stub, $name, $className) + protected function sharedStrReplace(string $stub, $name, $className): array|string { - $stub = str_replace('DummyNamespace', "App" . $this->getFormNamespace(), $stub); - $stub = str_replace('ModelsPath', $className, $stub); - $stub = str_replace('DummyModel', $name, $stub); - $stub = str_replace('dummymodel', Str::snake($name), $stub); - return $stub; + return str_replace( + ['DummyNamespace', 'ModelsPath', 'DummyModel', 'dummymodel'], + ["App" . $this->getFormNamespace(), $className, $name, Str::snake($name)], + $stub); } protected function getFormNamespace(): string diff --git a/src/Tasks/MethodsTrait.php b/src/Tasks/MethodsTrait.php index d92d909..ea92891 100644 --- a/src/Tasks/MethodsTrait.php +++ b/src/Tasks/MethodsTrait.php @@ -13,18 +13,18 @@ trait MethodsTrait { - protected $imports = []; - protected $data = []; - protected $statements; - protected $name = ''; - protected $action = ''; - protected $eloquentActions = [ + protected array $imports = []; + protected array $data = []; + protected mixed $statements; + protected string $name = ''; + protected string $action = ''; + protected array $eloquentActions = [ 'create' => '$this->model = DummyModel::create($validated_data);' . PHP_EOL . self::INDENT . '$dummymodel = $this->model;' . PHP_EOL . self::INDENT . '$this->showDelete = true;', 'update' => '$this->model->update($validated_data);' . PHP_EOL . self::INDENT . '$dummymodel = $this->model;', 'delete' => '$this->model->delete();' ]; - public function __construct($statements, array $data) + public function __construct(mixed $statements, array $data) { $this->statements = $statements; $this->name = data_get($data, 'name', ''); //$controller->name() @@ -48,7 +48,7 @@ protected function buildImports(): array return $imports; } - protected function addImport($class) + protected function addImport(string $class): void { $this->imports[$this->name][] = 'use ' . $class . ';'; } @@ -60,7 +60,7 @@ protected function makeEloquentStatement(string $statement): string return $string; } - private function buildMethods($statements): string + private function buildMethods(mixed $statements): string { $body = ''; foreach ($statements as $statement) { diff --git a/src/Tasks/OnCreate.php b/src/Tasks/OnCreate.php index 0751fb8..5999a23 100644 --- a/src/Tasks/OnCreate.php +++ b/src/Tasks/OnCreate.php @@ -7,20 +7,24 @@ class OnCreate { use MethodsTrait; - const INDENT = ' '; + protected const INDENT = ' '; //defaults - protected $session = null; //tall-forms has a notify() method that displays a success message on create/update - protected $redirect = null; //tall-forms has a save and go back button or save and stay. + protected ?string $session = null; //tall-forms has a notify() method that displays a success message on create/update + protected ?string $redirect = null; //tall-forms has a save and go back button or save and stay. - protected function redirect($string): void + protected function redirect(?string $string): void { - if(config('tall-forms-blueprint.resource-redirect')) $this->redirect = $string; + if(config('tall-forms-blueprint.resource-redirect')) { + $this->redirect = $string; + } } - protected function session($string): void + protected function session(?string $string): void { - if(config('tall-forms-blueprint.resource-session')) $this->session = $string; + if(config('tall-forms-blueprint.resource-session')) { + $this->session = $string; + } } } diff --git a/src/Tasks/OnDelete.php b/src/Tasks/OnDelete.php index 91c1b32..4ff9365 100644 --- a/src/Tasks/OnDelete.php +++ b/src/Tasks/OnDelete.php @@ -8,20 +8,20 @@ class OnDelete { use MethodsTrait; - const INDENT = ' '; + protected const INDENT = ' '; - protected $session = self::INDENT . 'session()->flash("success", "The "' . ' . class_basename($this->model). ' . '" was deleted");' . PHP_EOL; + protected string $session = self::INDENT . 'session()->flash("success", "The "' . ' . class_basename($this->model). ' . '" was deleted");' . PHP_EOL; - protected $redirect = self::INDENT . 'return redirect(urldecode($this->previous));' . PHP_EOL; + protected string $redirect = self::INDENT . 'return redirect(urldecode($this->previous));' . PHP_EOL; - protected function redirect($string): void + protected function redirect(string $string): void { //return redirect as set in draft.yaml delete requires a redirect //controllers resource shorthand auto generates a redirect $this->redirect = $string; } - protected function session($string): void + protected function session(string $string): void { //best to flash to session as deletes generates a redirect $this->session = $string; diff --git a/src/Tasks/OnUpdate.php b/src/Tasks/OnUpdate.php index 38c1f5c..7264772 100644 --- a/src/Tasks/OnUpdate.php +++ b/src/Tasks/OnUpdate.php @@ -8,20 +8,24 @@ class OnUpdate { use MethodsTrait; - const INDENT = ' '; + protected const INDENT = ' '; //defaults - protected $session = null; //tall-forms has a notify() method that displays a success message on create/update - protected $redirect = null; //tall-forms has a save and go back button or save and stay. + protected ?string $session = null; //tall-forms has a notify() method that displays a success message on create/update + protected ?string $redirect = null; //tall-forms has a save and go back button or save and stay. - protected function redirect($string): void + protected function redirect(?string $string): void { - if(config('tall-forms-blueprint.resource-redirect')) $this->redirect = $string; + if(config('tall-forms-blueprint.resource-redirect')) { + $this->redirect = $string; + } } - protected function session($string): void + protected function session(?string $string): void { - if(config('tall-forms-blueprint.resource-session')) $this->session = $string; + if(config('tall-forms-blueprint.resource-session')) { + $this->session = $string; + } } } diff --git a/src/Tasks/RemapImports.php b/src/Tasks/RemapImports.php index 9e2c5bd..4730121 100644 --- a/src/Tasks/RemapImports.php +++ b/src/Tasks/RemapImports.php @@ -12,11 +12,10 @@ public function handle(array $data, Closure $next): array $data['imports'] = collect($data['imports']) ->unique() ->map(function ($type) { - if(in_array($type, $this->freeFields())) { + if(in_array($type, $this->freeFields(), false)) { return 'use Tanthammar\TallForms\\'.$type.';'; - } else { - return 'use Tanthammar\TallFormsSponsors\\'.$type.';'; } + return 'use Tanthammar\TallFormsSponsors\\'.$type.';'; }) ->prepend('use Tanthammar\TallForms\TallFormComponent;') ->sort(function ($a, $b) { diff --git a/src/Translators/Rules.php b/src/Translators/Rules.php index cf92c7d..be527ec 100644 --- a/src/Translators/Rules.php +++ b/src/Translators/Rules.php @@ -11,7 +11,7 @@ public static function fromColumn(string $tableName, Column $column): array { $rules = BlueprintRules::fromColumn($tableName, $column); - if (in_array('nullable', $column->modifiers())) { + if (in_array('nullable', $column->modifiers(), false)) { $rules = array_filter($rules, function ($rule) { return $rule !== 'required'; }); diff --git a/stubs/class.stub.php b/stubs/class.stub.php index 2a7d054..2d4dd51 100644 --- a/stubs/class.stub.php +++ b/stubs/class.stub.php @@ -17,7 +17,7 @@ public function mount(?DummyModel $dummymodel) protected function formAttr(): array { $dummymodel = $this->model; - + return [ 'formTitle' => 'Create & Edit DummyModel', 'wrapWithView' => true, //see https://github.com/tanthammar/tall-forms/wiki/installation/Wrapper-Layout @@ -30,26 +30,26 @@ protected function formAttr(): array // REQUIRED, if you are creating a model with this form - protected function onCreateModel($validated_data) + protected function onCreateModel($validated_data): mixed { // create... } // OPTIONAL, method exists in tall-form component - protected function onUpdateModel($validated_data) + protected function onUpdateModel($validated_data): mixed { // update... } // OPTIONAL, method exists in tall-form component - protected function onDeleteModel() + protected function onDeleteModel(): mixed { $dummymodel = $this->model; // delete... } - protected function fields() + protected function fields(): array { return [ // fields...