Skip to content

Commit

Permalink
typhints
Browse files Browse the repository at this point in the history
  • Loading branch information
tanthammar committed Nov 18, 2022
1 parent d4df46c commit 3892f55
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 108 deletions.
22 changes: 8 additions & 14 deletions src/Generators/ViewGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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 {
Expand Down Expand Up @@ -80,16 +75,15 @@ 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';
}

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 = '<livewire:forms.' . $this->model . '-form :' . $this->model . '="$' . $this->model . '"/>';
$stub = str_replace('{{ view }} template', $template, $stub);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/HasStubPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ trait HasStubPath
{
/**
* Returns the stub path for this package.
*
* @return string
*/
protected function stubPath(): string
{
Expand Down
8 changes: 3 additions & 5 deletions src/TallBlueprintAddonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
15 changes: 6 additions & 9 deletions src/TallBlueprintGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}


Expand Down
25 changes: 13 additions & 12 deletions src/TallMethodsBlueprintGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/AddIdentifierField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AddIdentifierField implements Task
{
use InteractWithRelationships;

const INDENT = ' ';
protected const INDENT = ' ';

public function handle($data, Closure $next): array
{
Expand Down
16 changes: 10 additions & 6 deletions src/Tasks/AddRegularFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)';
}

Expand All @@ -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)';
}

Expand Down Expand Up @@ -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',
Expand Down
24 changes: 9 additions & 15 deletions src/Tasks/AddRelationshipFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AddRelationshipFields implements Task
{
use InteractWithRelationships;

const INDENT = ' ';
protected const INDENT = ' ';

public function handle(array $data, Closure $next): array
{
Expand Down Expand Up @@ -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)) {
Expand All @@ -85,15 +79,15 @@ 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',
'hasmany',
'morphmany',
];

return in_array(strtolower($type), $pluralRelations)
return in_array(strtolower($type), $pluralRelations, false)
? Str::plural($name)
: $name;
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/AddTimestampFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class AddTimestampFields implements Task
{
const INDENT = ' ';
protected const INDENT = ' ';

public function handle($data, Closure $next): array
{
Expand Down
11 changes: 5 additions & 6 deletions src/Tasks/HasSharedGeneratorFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/Tasks/MethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 . ';';
}
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 3892f55

Please sign in to comment.