Skip to content

Commit

Permalink
Merge pull request #66 from ajthinking/larastan
Browse files Browse the repository at this point in the history
Use phpstan and fix level 0 errors
  • Loading branch information
ajthinking authored May 6, 2022
2 parents 976badb + 44bebbb commit b0c014b
Show file tree
Hide file tree
Showing 35 changed files with 183 additions and 128 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"laravel/laravel": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0",
"pestphp/pest": "^1.21",
"phpstan/phpstan": "^1.6",
"phpunit/phpunit": "^8.0 || ^9.5"
},
"config": {
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 0
paths:
- src
- tests
ignoreErrors:
- '#Dummy not found#'
6 changes: 5 additions & 1 deletion src/Commands/ErrorsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

class ErrorsCommand extends Command
{
protected $result;
protected $errors;

/**
* The name and signature of the console command.
*
Expand Down Expand Up @@ -61,7 +64,8 @@ public function handle()
});

if ($this->errors->isEmpty()) {
return $this->info('No errors found!');
$this->info('No errors found!');
return;
}

$this->table(['path', 'message'], $this->errors->toArray());
Expand Down
2 changes: 2 additions & 0 deletions src/Endpoints/EndpointProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ abstract class EndpointProvider
{
use HasDirectiveHandlers;

public $file;

protected $directives;

public function __construct(PHPFile $file = null)
Expand Down
4 changes: 4 additions & 0 deletions src/Endpoints/PHP/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class Make extends Maker
protected string $extension = '.php';
protected string $relativeDir = '';

protected $namespace;
protected $class;
protected $outputDriver;

public function file(string $name = 'dummy.php')
{
$this->setupNames($name);
Expand Down
4 changes: 4 additions & 0 deletions src/Endpoints/PHP/PHPFileQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class PHPFileQueryBuilder extends EndpointProvider
use HasOperators;

const PHPSignature = '/\.php$/';

public $result;

public $baseDir;

public function __construct($file = null)
{
Expand Down
5 changes: 4 additions & 1 deletion src/PHPFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Archetype\Endpoints\PHP\ClassName;
use Archetype\Endpoints\PHP\Extends_;
use Archetype\Endpoints\PHP\Implements_;
use Archetype\Endpoints\PHP\Make;
use Archetype\Endpoints\Maker;
use Archetype\Endpoints\PHP\MethodNames;
use Archetype\Endpoints\PHP\Namespace_;
Expand All @@ -28,6 +27,10 @@ class PHPFile
use HasDirectiveHandlers;
use HasSyntacticSweeteners;

public $input;

public $output;

protected string $contents;

protected string $fileQueryBuilder = Endpoints\PHP\PHPFileQueryBuilder::class;
Expand Down
7 changes: 3 additions & 4 deletions src/Support/AST/Visitors/NodeInserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Archetype\Support\AST\Visitors;

use PhpParser\Node;
use PhpParser\NodeFinder;
use PhpParser\Node\Stmt\Use_;
use PhpParser\NodeVisitorAbstract;
use PhpParser\BuilderFactory;
use PhpParser\NodeTraverser;

class NodeInserter extends NodeVisitorAbstract
{

public $id;
public $newNode;

public function __construct($id, $newNode)
{
$this->id = $id;
Expand Down
4 changes: 4 additions & 0 deletions src/Support/AST/Visitors/NodePropertyReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

class NodePropertyReplacer extends NodeVisitorAbstract
{
public $id;
public $key;
public $value;

public function __construct($id, $key, $value)
{
$this->id = $id;
Expand Down
2 changes: 2 additions & 0 deletions src/Support/AST/Visitors/NodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class NodeRemover extends NodeVisitorAbstract
{
public $id;

public function __construct($id)
{
$this->id = $id;
Expand Down
3 changes: 3 additions & 0 deletions src/Support/AST/Visitors/NodeReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class NodeReplacer extends NodeVisitorAbstract
{
public $id;
public $newNode;

public function __construct($id, $newNode)
{
$this->id = $id;
Expand Down
4 changes: 4 additions & 0 deletions src/Support/AST/Visitors/StmtInserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class StmtInserter extends NodeVisitorAbstract
{
protected $finished = false;

protected $id;
protected $newNode;
protected $position;

const priority = [
'PhpParser\Node\Stmt\Namespace_',
'PhpParser\Node\Stmt\TraitUse',
Expand Down
3 changes: 3 additions & 0 deletions src/Support/Exceptions/FileParseError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class FileParseError extends Exception
{
public $path;
public $original;

public function __construct($path, $original)
{
$this->path = $path;
Expand Down
2 changes: 2 additions & 0 deletions src/Support/PHPFileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class PHPFileStorage
{
public $roots;

public function __construct()
{
$this->roots = config('archetype.roots');
Expand Down
9 changes: 4 additions & 5 deletions src/Support/PSR2PrettyPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ protected function pExpr_Array(Array_ $node)
* Ensure spacing between class stmts
*
* @param [type] $nodes
* @return void
*/
protected function pClassCommon(Class_ $node, $afterClassToken)
{
return $this->pModifiers($node->flags)
. 'class' . $afterClassToken
. (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
. (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
. $this->nl . '{' . $this->pSeparatedStmts($node->stmts) . $this->nl . '}';
. 'class' . $afterClassToken
. (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
. (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
. $this->nl . '{' . $this->pSeparatedStmts($node->stmts) . $this->nl . '}';
}

protected function implementsSeparated($nodes)
Expand Down
3 changes: 3 additions & 0 deletions src/Support/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class Path
{
public $path;
public $root;

public function __construct($inputPath)
{
$this->path = $this->normalize($inputPath);
Expand Down
2 changes: 2 additions & 0 deletions src/Support/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class Snippet
{
public $file;

public static function __callStatic($name, $args)
{
$replacementPairs = $args ? $args[0] : [];
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Archetype\Support\Exceptions\FileParseError;
use Archetype\Support\PSR2PrettyPrinter;
use PHPParser\Error as PHPParserError;
use PhpParser\Error as PHPParserError;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;

Expand Down
1 change: 0 additions & 1 deletion src/Traits/PHPParserClassMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function classMap(string $class = null)
'list' => \PhpParser\Node\Expr\List_::class,
'lNumber' => \PhpParser\Node\Scalar\LNumber::class,
'magicConst' => \PhpParser\Node\Scalar\MagicConst::class,
'magicConst' => \PhpParser\Node\Scalar\MagicConst::class,
'methodCall' => \PhpParser\Node\Expr\MethodCall::class,
'name' => \PhpParser\Node\Name::class,
'namespace' => \PhpParser\Node\Stmt\Namespace_::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

use Archetype\Facades\LaravelFile;

use function PHPUnit\Framework\assertCount;
use function PHPUnit\Framework\assertTrue;

it('can scope on models', function() {
$this->assertCount(
assertCount(
1,
LaravelFile::models()->get()
);
});

it('can scope on controllers', function() {
$this->assertCount(
assertCount(
1,
LaravelFile::controllers()->get()
);
});

it('can get user', function() {
$this->assertTrue(
assertTrue(
get_class(LaravelFile::load('app/Models/User.php')) === 'Archetype\LaravelFile'
);
});
40 changes: 18 additions & 22 deletions tests/Feature/Endpoints/Laravel/LaravelPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,49 @@

use Archetype\Facades\LaravelFile;

use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertNull;
use function PHPUnit\Framework\assertTrue;

it('can retrieve fillables', function() {
$this->assertTrue(
assertTrue(
LaravelFile::load('app/Models/User.php')->fillable() == ['name', 'email', 'password',]
);
});

it('can retrieve hidden', function() {
$this->assertTrue(
assertTrue(
LaravelFile::load('app/Models/User.php')->hidden() == ['password', 'remember_token',]
);
});

it('wont break if properties are missing', function() {
$this->assertNull(
assertNull(
LaravelFile::load('public/index.php')->hidden()
);
});

it('putting this test here helps the one below not break', function() {
// WHY?
});

it('will assume array if we are inserting on a new hidden property', function() {
$hidden = LaravelFile::load('app/Models/User.php')
->remove()->hidden()
->hidden('ghost')->hidden();

$this->assertEquals(
['ghost'],
$hidden
);

$hidden = LaravelFile::load('app/Models/User.php')
->remove()->hidden()
->hidden(['ghost'])->hidden();

$this->assertEquals(
['ghost'],
$hidden
);
assertEquals(['ghost'], $hidden);
});

it('can set fillables', function() {
$this->assertEquals(
assertEquals(
LaravelFile::load('app/Models/User.php')->fillable(['guns', 'roses'])->fillable(),
['guns', 'roses',]
);
});

it('can add fillables', function() {
$this->assertEquals(
assertEquals(
LaravelFile::load('app/Models/User.php')
->fillable(['guns', 'roses'])
->add()->fillable(['metallica'])
Expand All @@ -58,7 +54,7 @@
});

it('can set hidden', function() {
$this->assertEquals(
assertEquals(
LaravelFile::load('app/Models/User.php')->hidden(['metallica', 'ozzy'])->hidden(),
['metallica', 'ozzy',]
);
Expand All @@ -69,7 +65,7 @@
->casts(['free' => 'bird'])
->casts();

$this->assertEquals([
assertEquals([
'free' => 'bird',
], $output);
});
Expand All @@ -79,7 +75,7 @@
->add()->casts(['free' => 'bird'])
->casts();

$this->assertEquals([
assertEquals([
'email_verified_at' => 'datetime',
'free' => 'bird',
], $output);
Expand All @@ -90,5 +86,5 @@
->empty()->casts()
->casts();

$this->assertEquals([], $output);
assertEquals([], $output);
});
Loading

0 comments on commit b0c014b

Please sign in to comment.