Skip to content

Commit

Permalink
Add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Nov 24, 2023
1 parent f2730bd commit 45f0e37
Show file tree
Hide file tree
Showing 57 changed files with 339 additions and 468 deletions.
16 changes: 8 additions & 8 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

class Blueprint
{
private $lexers = [];
private array $lexers = [];

private $generators = [];
private array $generators = [];

public static function relativeNamespace(string $fullyQualifiedClassName)
public static function relativeNamespace(string $fullyQualifiedClassName): string
{
$namespace = config('blueprint.namespace') . '\\';
$reference = ltrim($fullyQualifiedClassName, '\\');
Expand Down Expand Up @@ -73,7 +73,7 @@ public function parse($content, $strip_dashes = true)
return Yaml::parse($content);
}

public function analyze(array $tokens)
public function analyze(array $tokens): Tree
{
$registry = [
'models' => [],
Expand All @@ -100,22 +100,22 @@ public function generate(Tree $tree, array $only = [], array $skip = [], $overwr
return $components;
}

public function dump(array $generated)
public function dump(array $generated): string
{
return Yaml::dump($generated);
}

public function registerLexer(Lexer $lexer)
public function registerLexer(Lexer $lexer): void
{
$this->lexers[] = $lexer;
}

public function registerGenerator(Generator $generator)
public function registerGenerator(Generator $generator): void
{
$this->generators[] = $generator;
}

public function swapGenerator(string $concrete, Generator $generator)
public function swapGenerator(string $concrete, Generator $generator): void
{
foreach ($this->generators as $key => $registeredGenerator) {
if (get_class($registeredGenerator) === $concrete) {
Expand Down
12 changes: 3 additions & 9 deletions src/BlueprintServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class BlueprintServiceProvider extends ServiceProvider implements DeferrableProv
{
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
public function boot(): void
{
if (!defined('STUBS_PATH')) {
define('STUBS_PATH', dirname(__DIR__) . '/stubs');
Expand All @@ -42,10 +40,8 @@ public function boot()

/**
* Register the service provider.
*
* @return void
*/
public function register()
public function register(): void
{
$this->mergeConfigFrom(
__DIR__ . '/../config/blueprint.php',
Expand Down Expand Up @@ -93,10 +89,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
2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Builder
{
public function execute(Blueprint $blueprint, Filesystem $filesystem, string $draft, string $only = '', string $skip = '', $overwriteMigrations = false)
public function execute(Blueprint $blueprint, Filesystem $filesystem, string $draft, string $only = '', string $skip = '', $overwriteMigrations = false): array
{
$cache = [];
if ($filesystem->exists('.blueprint')) {
Expand Down
16 changes: 6 additions & 10 deletions src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ class BuildCommand extends Command
*/
protected $description = 'Build components from a Blueprint draft';

/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;

/** @var Builder */
private $builder;
private Builder $builder;

public function __construct(Filesystem $filesystem, Builder $builder)
{
Expand All @@ -44,7 +42,7 @@ public function __construct(Filesystem $filesystem, Builder $builder)
$this->builder = $builder;
}

public function handle()
public function handle(): int
{
$file = $this->argument('draft') ?? $this->defaultDraftFile();

Expand Down Expand Up @@ -79,17 +77,15 @@ function ($file) {

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
protected function getArguments(): array
{
return [
['draft', InputArgument::OPTIONAL, 'The path to the draft file, default: draft.yaml or draft.yml', []],
];
}

private function outputStyle($action)
private function outputStyle(string $action): string
{
if ($action === 'deleted') {
return 'error';
Expand All @@ -100,7 +96,7 @@ private function outputStyle($action)
return 'info';
}

private function defaultDraftFile()
private function defaultDraftFile(): string
{
return file_exists('draft.yml') ? 'draft.yml' : 'draft.yaml';
}
Expand Down
7 changes: 3 additions & 4 deletions src/Commands/EraseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class EraseCommand extends Command
*/
protected $description = 'Erase components created from last Blueprint build';

/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;

public function __construct(Filesystem $filesystem)
{
Expand All @@ -32,7 +31,7 @@ public function __construct(Filesystem $filesystem)
$this->filesystem = $filesystem;
}

public function handle()
public function handle(): int
{
$contents = $this->filesystem->get('.blueprint');

Expand Down Expand Up @@ -69,7 +68,7 @@ function ($file) {
return $this->call('blueprint:trace');
}

private function outputStyle($action)
private function outputStyle(string $action): string
{
if ($action === 'created') {
return 'error';
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InitCommand extends Command
*/
protected $description = 'An alias for "blueprint:new" command';

public function handle()
public function handle(): int
{
return $this->call(\Blueprint\Commands\NewCommand::class);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class NewCommand extends Command
*/
protected $description = 'Create a draft.yaml file and load existing models';

/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;

public function __construct(Filesystem $filesystem)
{
Expand All @@ -34,7 +33,7 @@ public function __construct(Filesystem $filesystem)
$this->filesystem = $filesystem;
}

public function handle()
public function handle(): int
{
if (!$this->filesystem->exists('draft.yaml')) {
$this->filesystem->put('draft.yaml', $this->filesystem->stub('draft.stub'));
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/PublishStubsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PublishStubsCommand extends Command
*/
protected $description = 'Publish blueprint stubs';

public function handle()
public function handle(): int
{
return $this->call('vendor:publish', ['--tag' => 'blueprint-stubs']);
}
Expand Down
8 changes: 3 additions & 5 deletions src/Commands/TraceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ class TraceCommand extends Command
*/
protected $description = 'Create definitions for existing models to reference in new drafts';

/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;

/** @var Tracer */
private $tracer;
private Tracer $tracer;

public function __construct(Filesystem $filesystem, Tracer $tracer)
{
Expand All @@ -40,7 +38,7 @@ public function __construct(Filesystem $filesystem, Tracer $tracer)
$this->tracer = $tracer;
}

public function handle()
public function handle(): int
{
$blueprint = resolve(Blueprint::class);
$path = $this->option('path');
Expand Down
6 changes: 3 additions & 3 deletions src/Concerns/HandlesImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

trait HandlesImports
{
protected $imports = [];
protected array $imports = [];

protected function addImport(Model $model, $class)
protected function addImport(Model $model, $class): void
{
$this->imports[$model->name()][] = $class;
}

protected function buildImports(Model $model)
protected function buildImports(Model $model): string
{
return collect($this->imports[$model->name()] ?? [])
->map(fn ($class) => "use {$class};")
Expand Down
6 changes: 3 additions & 3 deletions src/Concerns/HandlesTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

trait HandlesTraits
{
protected $traits = [];
protected array $traits = [];

protected function addTrait(Model $model, $trait)
protected function addTrait(Model $model, $trait): void
{
$this->traits[$model->name()][] = $trait;
}

protected function buildTraits(Model $model)
protected function buildTraits(Model $model): string
{
if (empty($this->traits[$model->name()])) {
return '';
Expand Down
8 changes: 4 additions & 4 deletions src/Contracts/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

interface Model
{
public function name();
public function name(): string;

public function namespace();
public function namespace(): string;

public function fullyQualifiedNamespace();
public function fullyQualifiedNamespace(): string;

public function fullyQualifiedClassName();
public function fullyQualifiedClassName(): string;
}
8 changes: 4 additions & 4 deletions src/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class EnumType extends Type
{
const ENUM = 'enum';

protected $values = [];
protected array $values = [];

public function getSQLDeclaration(array $column, AbstractPlatform $platform)
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
$values = array_map(
fn ($val) => "'" . $val . "'",
Expand All @@ -35,12 +35,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
return $value;
}

public function getName()
public function getName(): string
{
return self::ENUM;
}

public static function extractOptions($definition)
public static function extractOptions($definition): array
{
$options = explode(',', preg_replace('/enum\((?P<options>(.*))\)/', '$1', $definition));

Expand Down
4 changes: 2 additions & 2 deletions src/FileMixins.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class FileMixins
{
private $stubs = [];
private array $stubs = [];

public function stub()
public function stub(): \Closure
{
return function ($path) {
if (!isset($this->stubs[$path])) {
Expand Down
11 changes: 6 additions & 5 deletions src/Generators/AbstractClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

use Blueprint\Blueprint;
use Blueprint\Contracts\Model;
use Blueprint\Tree;
use Illuminate\Filesystem\Filesystem;

class AbstractClassGenerator
{
public const INDENT = ' ';

protected $filesystem;
protected Filesystem $filesystem;

protected $tree;
protected Tree $tree;

protected $output = [];
protected array $output = [];

public function __construct(Filesystem $filesystem)
{
Expand All @@ -26,14 +27,14 @@ public function types(): array
return $this->types;
}

protected function getPath(Model $model)
protected function getPath(Model $model): string
{
$path = str_replace('\\', '/', Blueprint::relativeNamespace($model->fullyQualifiedClassName()));

return sprintf('%s/%s.php', $this->basePath ?? Blueprint::appPath(), $path);
}

protected function create(string $path, $content)
protected function create(string $path, $content): void
{
if (!$this->filesystem->exists(dirname($path))) {
$this->filesystem->makeDirectory(dirname($path), 0755, true);
Expand Down
Loading

0 comments on commit 45f0e37

Please sign in to comment.