Skip to content

Commit

Permalink
Merge pull request #77 from ajthinking/add-use-trait-stuff
Browse files Browse the repository at this point in the history
Basic useTrait()
  • Loading branch information
ajthinking authored Aug 24, 2022
2 parents c5f67d9 + 92e4de9 commit cfee026
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 52 deletions.
74 changes: 74 additions & 0 deletions src/Endpoints/PHP/UseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Archetype\Endpoints\PHP;

use Archetype\Endpoints\EndpointProvider;
use PhpParser\BuilderFactory;
use Illuminate\Support\Arr;

class UseTrait extends EndpointProvider
{
/**
* @example Get which traits a class uses
* @source $file->useTrait()
*/
public function useTrait($value = null)
{
if ($this->file->directive('add')) {
return $this->add($value);
}

if ($value === null) {
return $this->get();
}

return $this->set($value);
}

protected function get()
{
$r = $this->file->astQuery()
->class()
->traitUse()
->name()
->parts
->get()
->toArray();

return $r;
}

protected function add($newUseTraitNames)
{
return $this->file->astQuery()
->class()
->insertStmts(
collect(Arr::wrap($newUseTraitNames))
->reverse()
->map(function ($name) {
return $this->getUseTraitNode($name);
})->toArray()
)
->commit()
->end()
->continue();
}

protected function getUseTraitNode($name)
{
$factory = new BuilderFactory;
return $factory->useTrait($name)->getNode();
}

protected function set($newUseTraitNames)
{
$this->file->astQuery()
->traitUse()
->remove()
->commit();

$this->add($newUseTraitNames);

return $this->file;
}
}
55 changes: 31 additions & 24 deletions src/PHPFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Archetype\Endpoints\PHP\Property;
use Archetype\Endpoints\PHP\ReflectionProxy;
use Archetype\Endpoints\PHP\Use_;
use Archetype\Endpoints\PHP\UseTrait;
use Archetype\Support\AST\ASTQueryBuilder;
use Archetype\Support\Types;
use Archetype\Traits\HasDirectives;
Expand All @@ -24,44 +25,44 @@

class PHPFile
{
use HasIO;
use HasDirectives;
use HasDirectiveHandlers;
use HasIO;
use HasDirectives;
use HasDirectiveHandlers;
use HasSyntacticSweeteners;

public InputInterface $input;

public OutputInterface $output;

protected string $contents;
protected string $contents;

protected string $fileQueryBuilder = Endpoints\PHP\PHPFileQueryBuilder::class;
protected string $fileQueryBuilder = Endpoints\PHP\PHPFileQueryBuilder::class;

protected Maker $maker;

public string $astQueryBuilder = ASTQueryBuilder::class;

protected $ast;
protected $ast;

protected string $initialModificationHash;
protected string $initialModificationHash;

protected $originalAst;
protected $originalAst;

protected $tokens;
protected $tokens;

protected $lexer;
protected $lexer;

protected $directives = [];
protected $directives = [];

public function __construct(
public function __construct(
InputInterface $input,
OutputInterface $output,
Maker $maker
) {
$this->input = $input;
$this->output = $output;
$this->maker = $maker;
}
}

public function query()
{
Expand All @@ -77,11 +78,11 @@ public function in(...$args)
{
return $this->query()->in(...$args);
}

public function where(...$args)
{
return $this->query()->where(...$args);
}
}

public function astQuery()
{
Expand All @@ -106,18 +107,24 @@ public function property($key, $value = Types::NO_VALUE)
return $handler->property($key, $value);
}

public function setProperty($key, $value = Types::NO_VALUE)
{
public function setProperty($key, $value = Types::NO_VALUE)
{
$handler = new Property($this);
return $handler->setProperty($key, $value);
}
return $handler->setProperty($key, $value);
}

public function use($value = null)
{
$handler = new Use_($this);
return $handler->use($value);
}

public function useTrait($value = null)
{
$handler = new UseTrait($this);
return $handler->useTrait($value);
}

public function namespace(string $value = null)
{
$handler = new Namespace_($this);
Expand All @@ -129,7 +136,7 @@ public function methodNames()
$handler = new MethodNames($this);
return $handler->methodNames();
}

public function implements($name = null)
{
$handler = new Implements_($this);
Expand All @@ -141,13 +148,13 @@ public function extends($name = null)
$handler = new Extends_($this);
return $handler->extends($name);
}

public function className($name = null)
{
$handler = new ClassName($this);
return $handler->className($name);
}

public function classConstant($key, $value = Types::NO_VALUE)
{
$handler = new ClassConstant($this);
Expand All @@ -158,5 +165,5 @@ public function setClassConstant($key, $value = Types::NO_VALUE)
{
$handler = new ClassConstant($this);
return $handler->setClassConstant($key, $value);
}
}
}
41 changes: 41 additions & 0 deletions tests/Feature/Endpoints/PHP/UseTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Archetype\Tests\Support\Facades\TestablePHPFile as PHPFile;

it('can retrieve class trait use statements', function () {
PHPFile::load('app/Models/User.php')
->assertUseTrait([
'HasApiTokens',
'HasFactory',
'Notifiable',
]);
});

it('can retrieve class trait use statements with mixed grouping', function () {
PHPFile::fromString('class X { use A, B; use C; }')
->assertUseTrait(['A', 'B', 'C']);
});

it('overwrites existing statements when setting a single value', function () {
PHPFile::fromString('class X { use A, B; use C; }')
->useTrait('NewTrait')
->assertUseTrait(['NewTrait']); //->assertBeautifulPhp();
});

it('overwrites existing statements when setting many values', function () {
PHPFile::fromString('class X { use A, B; use C; }')
->useTrait(['D', 'E'])
->assertUseTrait(['D', 'E']); //->assertBeautifulPhp();
});

it('can add class use trait statements by unshifting (!)', function () {
PHPFile::fromString('class X { use A; }')
->add()->useTrait('B')
->assertUseTrait(['B', 'A']); //->assertBeautifulPhp();
});

it('can add multiple class use trait statements', function () {
PHPFile::fromString('class X { use A; }')
->add()->useTrait(['B', 'C'])
->assertUseTrait(['B', 'C', 'A']); //->assertBeautifulPhp();
});
Loading

0 comments on commit cfee026

Please sign in to comment.