Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Oct 4, 2024
1 parent d363866 commit b34ac59
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/Actions/TransformTypesAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ public function transformClassNode(
if (count($node->getAttributes(Hidden::class)) > 0) {
return null;
}
$transformationContext = TransformationContext::createFromPhpClass($node);

foreach ($transformers as $transformer) {
$transformed = $transformer->transform(
$node,
TransformationContext::createFromPhpClass($node),
$transformationContext,
);

if ($transformed instanceof Transformed) {
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/Watch/DirectoryDeletedWatchEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class DirectoryDeletedWatchEventHandler implements WatchEventHandler
{
public function handle($event): void
{

// TODO: Implement handle() method.
}
}
63 changes: 62 additions & 1 deletion src/PhpNodes/PhpAttributeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Spatie\TypeScriptTransformer\PhpNodes;

use ReflectionAttribute;
use ReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionAttribute as RoaveReflectionAttribute;

class PhpAttributeNode
{
protected ?array $arguments = null;

public function __construct(
public readonly ReflectionAttribute|RoaveReflectionAttribute $reflection
) {
Expand All @@ -22,9 +25,27 @@ public function getArguments(): array
return $this->reflection->getArguments();
}

public function hasArgument(string $name): bool
{
if ($this->arguments === null) {
$this->initializeArguments();
}

return array_key_exists($name, $this->arguments);
}

public function getArgument(string $name): mixed
{
if ($this->arguments === null) {
$this->initializeArguments();
}

return $this->arguments[$name] ?? null;
}

public function newInstance(): object
{
if($this->reflection instanceof ReflectionAttribute) {
if ($this->reflection instanceof ReflectionAttribute) {
return $this->reflection->newInstance();
}

Expand All @@ -33,4 +54,44 @@ public function newInstance(): object
// TODO: maybe we can do a little better here
return (new $className())($this->reflection->getArguments());
}

/** @return array<string, mixed> */
protected function initializeArguments(): array
{
// TODO: this is a quickly written thing, test it to be sure it works
if ($this->arguments !== null) {
return $this->arguments;
}

$this->arguments = [];

$values = $this->getArguments();

foreach ($values as $name => $value) {
if (is_string($name)) {
$this->arguments[$name] = $value;
unset($values[$name]);
}
}

if (count($values) === 0) {
return $this->arguments;
}

$constructor = new ReflectionMethod($this->reflection->getName(), '__construct');

foreach ($constructor->getParameters() as $index => $param) {
if(array_key_exists($param->getName(), $this->arguments)) {
continue;
}

if(! array_key_exists($index, $values)) {
continue;
}

$this->arguments[$param->getName()] = $values[$index];
}

return $this->arguments;
}
}
10 changes: 7 additions & 3 deletions src/Support/TransformationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ public function __construct(
public static function createFromPhpClass(
PhpClassNode $node
): TransformationContext {
$attributeArguments = ($node->getAttributes(TypeScript::class)[0] ?? null)?->getArguments() ?? [];
$attribute = $node->getAttributes(TypeScript::class)[0] ?? null;

$name = $attributeArguments['name'] ?? $node->getShortName();
$name = $attribute && $attribute->hasArgument('name')
? $attribute->getArgument('name')
: $node->getShortName();

$nameSpaceSegments = $attributeArguments['location'] ?? explode('\\', $node->getNamespaceName());
$nameSpaceSegments = $attribute && $attribute->hasArgument('location')
? $attribute->getArgument('location')
: explode('\\', $node->getNamespaceName());

return new TransformationContext(
$name,
Expand Down
9 changes: 5 additions & 4 deletions src/Writers/ModuleWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ public function output(TransformedCollection $collection): array

$writtenFiles = [];

foreach ($locations as $location) {
if ($location->hasChanges() === false) {
continue;
}
// TODO: remove files which still exists due to previous run

foreach ($locations as $location) {
$writtenFiles[] = $this->writeLocation($location, $collection);
}

// TODO: we probably can be a bit smarter about this
// -> only write files which have changed

return $writtenFiles;
}

Expand Down
17 changes: 0 additions & 17 deletions tests/Actions/TransformTypesActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Spatie\TypeScriptTransformer\Actions\TransformTypesAction;
use Spatie\TypeScriptTransformer\PhpNodes\PhpClassNode;
use Spatie\TypeScriptTransformer\Support\TypeScriptTransformerLog;
use Spatie\TypeScriptTransformer\Tests\Fakes\TypesToProvide\HiddenAttributedClass;
use Spatie\TypeScriptTransformer\Tests\Fakes\TypesToProvide\SimpleClass;
use Spatie\TypeScriptTransformer\Tests\Fakes\TypesToProvide\StringBackedEnum;
Expand Down Expand Up @@ -52,19 +51,3 @@

expect($types)->toBeEmpty();
});

it('will log errors when a type cannot be reflected', function () {
$types = (new TransformTypesAction())->execute(
[
new AllClassTransformer(),
],
[
PhpClassNode::fromClassString('NonExistentClass'),
]
);

expect($types)->toBeEmpty();

expect(TypeScriptTransformerLog::resolve()->errorMessages)
->toHaveCount(1);
});

0 comments on commit b34ac59

Please sign in to comment.