Skip to content

Commit

Permalink
feat: support multiple paths
Browse files Browse the repository at this point in the history
feat: fixed nullish generators for docblocks
fix: general cleanups
removed: plasm
added: pint
wip phpstan
  • Loading branch information
nikuscs committed May 16, 2024
1 parent 839999e commit ce5f85b
Show file tree
Hide file tree
Showing 35 changed files with 239 additions and 189 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
},
"require-dev": {
"brianium/paratest": "^7.4",
"laravel/pint": "^1.15",
"nunomaduro/collision": "^8.1",
"orchestra/testbench": "^9.0",
"pestphp/pest": "^2.34",
"phpunit/phpunit": "^10.5",
"spatie/invade": "^2.0",
"spatie/laravel-ray": "^1.36",
"spatie/pest-plugin-snapshots": "^2.1",
"vimeo/psalm": "^5.24"
"spatie/pest-plugin-snapshots": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 10 additions & 2 deletions config/actions-ide-helper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php
// config for Wulfheart/ClassName
return [

return [
'paths' => [
app_path('Actions'),
base_path('Modules'),
base_path('modules'),
base_path('Domain'),
base_path('domain'),
base_path('src'),
],
'file_name' => base_path('_ide_helper_actions.php'),
];
29 changes: 14 additions & 15 deletions src/Commands/LaravelActionsIdeHelperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,36 @@

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use phpDocumentor\Reflection\File\LocalFile;
use PhpParser\BuilderFactory;
use PhpParser\PrettyPrinter\Standard;
use ReflectionClass;
use Riimu\Kit\PathJoin\Path;
use Symfony\Component\Finder\Finder;
use Wulfheart\LaravelActionsIdeHelper\ClassMapGenerator;
use Wulfheart\LaravelActionsIdeHelper\Service\ActionInfo;
use Wulfheart\LaravelActionsIdeHelper\Service\ActionInfoFactory;
use Wulfheart\LaravelActionsIdeHelper\Service\BuildIdeHelper;
use Wulfheart\LaravelActionsIdeHelper\Service\Generator\DocBlock\AsObjectGenerator;

class LaravelActionsIdeHelperCommand extends Command
{
public $signature = 'ide-helper:actions';

public $description = 'Generate a new IDE Helper file for Laravel Actions.';

public function handle()
public function handle(): int
{

$actionsPath = Path::join(app_path() . '/Actions');

$outfile = Path::join(base_path(), '/_ide_helper_actions.php');
$defaultActionsPaths = [
app_path('Actions'),
base_path('Modules'),
base_path('modules'),
base_path('Domain'),
base_path('src'),
];
$defaultOutputFile = base_path('_ide_helper_actions.php');
$actionsPath = config('laravel-actions-ide-helper.paths', $defaultActionsPaths);
$outfile = config('laravel-actions-ide-helper.file_name', $defaultOutputFile);

$actionInfos = ActionInfoFactory::create($actionsPath);

$result = BuildIdeHelper::create()->build($actionInfos);

file_put_contents($outfile, $result);

$this->comment('IDE Helpers generated for Laravel Actions at ' . Str::of($outfile));
$this->comment('IDE Helpers generated for Laravel Actions at '.Str::of($outfile));

return static::SUCCESS;
}
}
9 changes: 1 addition & 8 deletions src/LaravelActionsIdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@ class LaravelActionsIdeHelperServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package
->name('laravel-actions-ide-helper')
// ->hasConfigFile()
// ->hasViews()
// ->hasMigration('create_laravel-actions-ide-helper_table')
->hasConfigFile()
->hasCommand(LaravelActionsIdeHelperCommand::class);
}
}
19 changes: 12 additions & 7 deletions src/Service/ActionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Wulfheart\LaravelActionsIdeHelper\Service;

use Illuminate\Support\Str;
use JetBrains\PhpStorm\Pure;
use Lorisleiva\Actions\Concerns\AsCommand;
use Lorisleiva\Actions\Concerns\AsController;
use Lorisleiva\Actions\Concerns\AsFake;
Expand All @@ -16,17 +15,26 @@
use Wulfheart\LaravelActionsIdeHelper\Service\Generator\DocBlock\AsJobGenerator;
use Wulfheart\LaravelActionsIdeHelper\Service\Generator\DocBlock\AsListenerGenerator;
use Wulfheart\LaravelActionsIdeHelper\Service\Generator\DocBlock\AsObjectGenerator;
use Wulfheart\LaravelActionsIdeHelper\Service\Generator\DocBlock\DocBlockGeneratorInterface;

final class ActionInfo
{
public string $name;

public string $namespace;

public string $fqsen;

public bool $asObject;

public bool $asController;

public bool $asJob;

public bool $asListener;

public bool $asCommand;

public Class_ $classInfo;

const ALL_TRAITS = [
Expand All @@ -47,7 +55,7 @@ public function setName(string $name): ActionInfo
{
$this->fqsen = $name;
$this->name = class_basename($name);
$this->namespace = Str::of($name)->beforeLast('\\' . $this->name);
$this->namespace = Str::of($name)->beforeLast('\\'.$this->name);

return $this;
}
Expand Down Expand Up @@ -90,12 +98,12 @@ public function setAsCommand(bool $asCommand): ActionInfo
public function setClassInfo(Class_ $classInfo): ActionInfo
{
$this->classInfo = $classInfo;

return $this;
}


/**
* @return \Wulfheart\LaravelActionsIdeHelper\Service\Generator\DocBlock\DocBlockGeneratorInterface[]
* @return DocBlockGeneratorInterface[]
*/
public function getGenerators(): array
{
Expand All @@ -107,7 +115,4 @@ public function getGenerators(): array
($this->asObject ? [AsObjectGenerator::class] : []),
);
}



}
58 changes: 35 additions & 23 deletions src/Service/ActionInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,33 @@
use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsCommand;
use Lorisleiva\Actions\Concerns\AsController;
use Lorisleiva\Actions\Concerns\AsFake;
use Lorisleiva\Actions\Concerns\AsJob;
use Lorisleiva\Actions\Concerns\AsListener;
use Lorisleiva\Actions\Concerns\AsObject;
use Lorisleiva\Lody\Lody;
use phpDocumentor\Reflection\Exception;
use phpDocumentor\Reflection\File\LocalFile;
use phpDocumentor\Reflection\Php\Class_;
use phpDocumentor\Reflection\Php\File;
use phpDocumentor\Reflection\Php\Project;
use phpDocumentor\Reflection\Php\ProjectFactory;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

class ActionInfoFactory
{
/** @return array<\Wulfheart\LaravelActionsIdeHelper\Service\ActionInfo> */
public static function create(string $path): array
/**
* @return array<ActionInfo>
*/
public static function create(array $paths): array
{
$factory = new self();
$classes = $factory->loadFromPath($path);
$classMap = $factory->loadPhpDocumentorReflectionClassMap($path);
$validPaths = array_filter($paths, fn($path) => is_dir($path));
$classes = collect($validPaths)->flatMap(fn ($path) => $factory->loadFromPath($path))->toArray();
$classMap = collect($validPaths)->flatMap(fn ($path) => $factory->loadPhpDocumentorReflectionClassMap($path))->toArray();
$ais = [];
foreach ($classes as $class => $traits){
foreach ($classes as $class => $traits) {
$tc = collect($traits);
$reflection = new \ReflectionClass($class);
$ais[] = ActionInfo::create()
->setName($class)
->setAsObject($tc->contains(AsObject::class))
Expand All @@ -38,21 +42,26 @@ public static function create(string $path): array
->setClassInfo($classMap[$class]);
}
return $ais;


}

/** @return array<class-string,array<class-string>> */
protected function loadFromPath(string $path)
/**
* @return array<class-string,array<class-string>>
*/
protected function loadFromPath(string $path): array
{
if (! is_dir($path)) {
return [];
}

$res = Lody::classes($path)->isNotAbstract();

/** @var array<class-string,array<class-string>> $traits */
return collect(ActionInfo::ALL_TRAITS)
->map(fn($trait, $key) => [$trait => $res->hasTrait($trait)->all()])
->map(fn ($trait, $key) => [$trait => $res->hasTrait($trait)->all()])
->collapse()
->map(function ($item, $key) {
return collect($item)
->map(fn($i) => [
->map(fn ($i) => [
'item' => $i,
'group' => $key,
])
Expand All @@ -61,25 +70,28 @@ protected function loadFromPath(string $path)
->values()
->collapse()
->groupBy('item')
->map(fn($item) => $item->pluck('group')->toArray())
->map(fn ($item) => $item->pluck('group')->toArray())
->toArray();
}

/** @return array<\phpDocumentor\Reflection\Php\Class_>
* @throws \phpDocumentor\Reflection\Exception
/**
* @return array<Class_>
*
* @throws Exception
*/
protected function loadPhpDocumentorReflectionClassMap(string $path): array{
protected function loadPhpDocumentorReflectionClassMap(string $path): array
{
$finder = Finder::create()->files()->in($path)->name('*.php');
$files = collect($finder)->map(fn(SplFileInfo $file) => new LocalFile($file->getRealPath()))->toArray();
$files = collect($finder)->map(fn (SplFileInfo $file) => new LocalFile($file->getRealPath()))->toArray();

/** @var \phpDocumentor\Reflection\Php\Project $project */
/** @var Project $project */
$project = ProjectFactory::createInstance()->create('Laravel Actions IDE Helper', $files);

return collect($project->getFiles())
->map(fn(File $f) => $f->getClasses())
->map(fn (File $f) => $f->getClasses())
->collapse()
->mapWithKeys(fn($item, string $key) => [Str::of($key)->ltrim("\\")->toString() => $item])
->mapWithKeys(fn ($item, string $key) => [Str::of($key)->ltrim('\\')->toString() => $item])
->toArray();

}

}
}
17 changes: 7 additions & 10 deletions src/Service/BuildIdeHelper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Wulfheart\LaravelActionsIdeHelper\Service;

use phpDocumentor\Reflection\DocBlock;
Expand All @@ -21,7 +20,7 @@ public static function create(): BuildIdeHelper
}

/**
* @param \Wulfheart\LaravelActionsIdeHelper\Service\ActionInfo[] $actionInfos
* @param ActionInfo[] $actionInfos
*/
public function build(array $actionInfos): string
{
Expand All @@ -32,7 +31,7 @@ public function build(array $actionInfos): string
$nodes = [];
/**
* @var string $namespace
* @var \Wulfheart\LaravelActionsIdeHelper\Service\ActionInfo[] $items
* @var ActionInfo[] $items
*/
$factory = new BuilderFactory();
foreach ($groups as $namespace => $items) {
Expand All @@ -45,6 +44,7 @@ public function build(array $actionInfos): string
$nodes[] = $this->getTraitIdeHelpers($factory);
$printer = new Standard();
$data = $printer->prettyPrintFile($nodes);

return $data;
}

Expand All @@ -55,7 +55,6 @@ protected function generateDocBlocks(ActionInfo $info): string
$tags = array_merge($tags, $generator::create()->generate($info));
}


return $this->serializeDocBlocks(...$tags);
}

Expand All @@ -81,27 +80,27 @@ protected function getTraitIdeHelpers(BuilderFactory $factory): \PhpParser\Node
{
return $factory->namespace("Lorisleiva\Actions\Concerns")
->addStmt(
(new Trait_("AsController"))->setDocComment(
(new Trait_('AsController'))->setDocComment(
$this->serializeDocBlocks(
new DocBlock\Tags\Method('asController', [], $this->resolveType('void'))
)
)
)->addStmt(
(new Trait_("AsListener"))->setDocComment(
(new Trait_('AsListener'))->setDocComment(
$this->serializeDocBlocks(
new DocBlock\Tags\Method('asListener', [], $this->resolveType('void'))
)
)
)->addStmt(
(new Trait_("AsJob"))->setDocComment(
(new Trait_('AsJob'))->setDocComment(
$this->serializeDocBlocks(
new DocBlock\Tags\Method('asJob', [], $this->resolveType('void'))
)
)

)
->addStmt(
(new Trait_("AsCommand"))->setDocComment(
(new Trait_('AsCommand'))->setDocComment(
$this->serializeDocBlocks(
new DocBlock\Tags\Method('asCommand', arguments: [
['name' => 'command', 'type' => $this->resolveType("\Illuminate\Console\Command")],
Expand All @@ -111,7 +110,5 @@ protected function getTraitIdeHelpers(BuilderFactory $factory): \PhpParser\Node
)
->getNode();


}

}
4 changes: 2 additions & 2 deletions src/Service/Generator/DocBlock/AsCommandGenerator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Wulfheart\LaravelActionsIdeHelper\Service\Generator\DocBlock;

use Lorisleiva\Actions\Concerns\AsCommand;
Expand All @@ -9,8 +8,9 @@
class AsCommandGenerator extends DocBlockGeneratorBase implements DocBlockGeneratorInterface
{
protected string $context = AsCommand::class;

/**
* @inheritDoc
* {@inheritDoc}
*/
public function generate(ActionInfo $info): array
{
Expand Down
Loading

0 comments on commit ce5f85b

Please sign in to comment.