-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from ajthinking/add-use-trait-stuff
Basic useTrait()
- Loading branch information
Showing
4 changed files
with
184 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
Oops, something went wrong.