Skip to content

Commit 74132fe

Browse files
committed
refactor(commands): simplify machine class discovery logic
Replace custom directory scanning with Composer's autoload classmap for finding machine classes, improving maintainability and reducing complexity.
1 parent 180b473 commit 74132fe

File tree

1 file changed

+13
-62
lines changed

1 file changed

+13
-62
lines changed

src/Commands/MachineConfigValidatorCommand.php

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
namespace Tarfinlabs\EventMachine\Commands;
66

77
use Throwable;
8-
use ReflectionClass;
98
use InvalidArgumentException;
109
use Illuminate\Console\Command;
11-
use Illuminate\Support\Facades\File;
12-
use Symfony\Component\Finder\SplFileInfo;
1310
use Tarfinlabs\EventMachine\Actor\Machine;
1411
use Tarfinlabs\EventMachine\StateConfigValidator;
1512

@@ -68,7 +65,7 @@ protected function validateMachine(string $machineClass): void
6865
}
6966

7067
// Check if class exists and is a Machine
71-
if (!is_subclass_of(object_or_class: $fullClassName, class: Machine::class)) {
68+
if (!is_subclass_of($fullClassName, class: Machine::class)) {
7269
$this->error(string: "Class '{$fullClassName}' is not a Machine.");
7370

7471
return;
@@ -87,10 +84,10 @@ protected function validateMachine(string $machineClass): void
8784

8885
} catch (InvalidArgumentException $e) {
8986
$this->error(string: "Configuration error in '{$fullClassName}':");
90-
$this->error(string: $e->getMessage());
87+
$this->error($e->getMessage());
9188
} catch (Throwable $e) {
9289
$this->error(string: "Error validating '{$machineClass}':");
93-
$this->error(string: $e->getMessage());
90+
$this->error($e->getMessage());
9491
}
9592
}
9693

@@ -126,75 +123,29 @@ protected function findMachineClass(string $class): ?string
126123
}
127124

128125
/**
129-
* Find all potential machine classes in the project.
126+
* Find all machine classes.
130127
*
131128
* @return array<string>
132129
*/
133130
protected function findMachineClasses(): array
134131
{
135132
$machineClasses = [];
136133

137-
// Get package path
138-
$packagePath = (new ReflectionClass(objectOrClass: Machine::class))->getFileName();
139-
$packagePath = dirname($packagePath, levels: 3); // Go up to package root
140-
141-
// Check tests directory
142-
$testsPath = $packagePath.'/tests';
143-
if (File::exists($testsPath)) {
144-
$this->findMachineClassesInDirectory(directory: $testsPath, machineClasses: $machineClasses);
145-
}
134+
try {
135+
$classMap = require base_path('vendor/composer/autoload_classmap.php');
146136

147-
// Check src directory
148-
$srcPath = $packagePath.'/src';
149-
if (File::exists($srcPath)) {
150-
$this->findMachineClassesInDirectory(directory: $srcPath, machineClasses: $machineClasses);
137+
foreach ($classMap as $class => $path) {
138+
if (class_exists($class) && is_subclass_of($class, class: Machine::class)) {
139+
$machineClasses[] = $class;
140+
}
141+
}
142+
} catch (Throwable $e) {
143+
$this->error(string: 'Error scanning for machine classes: '.$e->getMessage());
151144
}
152145

153146
return array_values(array_unique($machineClasses));
154147
}
155148

156-
/**
157-
* Find machine classes in a specific directory.
158-
*
159-
* @param array<string> $machineClasses
160-
*/
161-
protected function findMachineClassesInDirectory(string $directory, array &$machineClasses): void
162-
{
163-
// Find all PHP files recursively
164-
$files = File::allFiles($directory);
165-
166-
foreach ($files as $file) {
167-
if (!$file instanceof SplFileInfo) {
168-
continue;
169-
}
170-
171-
// Get file contents
172-
$contents = File::get($file->getRealPath());
173-
174-
// Extract namespace
175-
preg_match(pattern: '/namespace\s+([^;]+)/i', subject: $contents, matches: $namespaceMatches);
176-
if (empty($namespaceMatches[1])) {
177-
continue;
178-
}
179-
$namespace = trim($namespaceMatches[1]);
180-
181-
// Extract class name
182-
preg_match(pattern: '/class\s+(\w+)/i', subject: $contents, matches: $classMatches);
183-
if (empty($classMatches[1])) {
184-
continue;
185-
}
186-
$className = trim($classMatches[1]);
187-
188-
// Create FQN
189-
$fqn = $namespace.'\\'.$className;
190-
191-
// Check if class exists and is a Machine
192-
if (class_exists($fqn) && is_subclass_of($fqn, Machine::class)) {
193-
$machineClasses[] = $fqn;
194-
}
195-
}
196-
}
197-
198149
/**
199150
* Validate all machines in the project.
200151
*/

0 commit comments

Comments
 (0)