Skip to content

Commit

Permalink
Merge branch 'release/0.9.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Sep 5, 2018
2 parents 4f5b6c0 + 502fe59 commit ed3c32c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 8 deletions.
29 changes: 26 additions & 3 deletions src/CodeCleaner/ListPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
namespace Psy\CodeCleaner;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use Psy\Exception\ParseErrorException;

Expand Down Expand Up @@ -74,9 +77,7 @@ public function enterNode(Node $node)
throw new ParseErrorException($msg, $item->key->getLine());
}

$value = ($item instanceof ArrayItem) ? $item->value : $item;

if (!$value instanceof Variable) {
if (!self::isValidArrayItem($item)) {
$msg = 'Assignments can only happen to writable values';
throw new ParseErrorException($msg, $item->getLine());
}
Expand All @@ -86,4 +87,26 @@ public function enterNode(Node $node)
throw new ParseErrorException('Cannot use empty list');
}
}

/**
* Validate whether a given item in an array is valid for short assignment.
*
* @param Expr $item
*
* @return bool
*/
private static function isValidArrayItem(Expr $item)
{
$value = ($item instanceof ArrayItem) ? $item->value : $item;

if ($value instanceof Variable) {
return true;
}

if ($value instanceof ArrayDimFetch || $value instanceof PropertyFetch) {
return isset($value->var) && $value->var instanceof Variable;
}

return false;
}
}
5 changes: 2 additions & 3 deletions src/CodeCleaner/RequirePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ public static function resolve($file, $lineNumber = null)
// So we're duplicating some of the logics here.
if (E_WARNING & \error_reporting()) {
ErrorException::throwException(E_WARNING, 'Filename cannot be empty', null, $lineNumber);
} else {
// @todo trigger an error as fallback? this is pretty ugly…
// trigger_error('Filename cannot be empty', E_USER_WARNING);
}
// @todo trigger an error as fallback? this is pretty ugly…
// trigger_error('Filename cannot be empty', E_USER_WARNING);
}

if ($file === '' || !\stream_resolve_include_path($file)) {
Expand Down
17 changes: 16 additions & 1 deletion src/CodeCleaner/ValidClassNamePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ protected function ensureClassOrInterfaceExists($name, $stmt)
}
}

/**
* Ensure that a referenced class _or trait_ exists.
*
* @throws FatalErrorException
*
* @param string $name
* @param Stmt $stmt
*/
protected function ensureClassOrTraitExists($name, $stmt)
{
if (!$this->classExists($name) && !$this->traitExists($name)) {
throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt);
}
}

/**
* Ensure that a statically called method exists.
*
Expand All @@ -259,7 +274,7 @@ protected function ensureClassOrInterfaceExists($name, $stmt)
*/
protected function ensureMethodExists($class, $name, $stmt)
{
$this->ensureClassExists($class, $stmt);
$this->ensureClassOrTraitExists($class, $stmt);

// let's pretend all calls to self, parent and static are valid
if (\in_array(\strtolower($class), ['self', 'parent', 'static'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*/
class Shell extends Application
{
const VERSION = 'v0.9.7';
const VERSION = 'v0.9.8';

const PROMPT = '>>> ';
const BUFF_PROMPT = '... ';
Expand Down
20 changes: 20 additions & 0 deletions test/CodeCleaner/Fixtures/TraitWithStatic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Psy\Test\CodeCleaner\Fixtures;

trait TraitWithStatic
{
public static function doStuff()
{
// Don't actually do stuff.
}
}
5 changes: 5 additions & 0 deletions test/CodeCleaner/ListPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function invalidStatements()
['[] = []', $errorShortListAssign],
['[$a] = [1]', $errorShortListAssign],
['list("a" => $a) = array("a" => 1)', $errorAssocListAssign],
['[$a[0], $a[1]] = [1, 2]', $errorShortListAssign],
['[$a->b, $a->c] = [1, 2]', $errorShortListAssign],
]);
}

Expand All @@ -64,6 +66,7 @@ public function invalidStatements()
['["a"] = [1]', $errorNonVariableAssign],
['[] = []', $errorEmptyList],
['[,] = [1,2]', $errorEmptyList],
['[,,] = [1,2,3]', $errorEmptyList],
]);
}

Expand Down Expand Up @@ -96,6 +99,8 @@ public function validStatements()
['[,$b] = [1,2,3]'],
['[$a,,$c] = [1,2,3]'],
['[$a,,,] = [1,2,3]'],
['[$a[0], $a[1]] = [1, 2]'],
['[$a->b, $a->c] = [1, 2]'],
]);
}

Expand Down
1 change: 1 addition & 0 deletions test/CodeCleaner/ValidClassNamePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class Kappa {}
['DateTime::$someMethod()'],
['Psy\Test\CodeCleaner\Fixtures\ClassWithStatic::doStuff()'],
['Psy\Test\CodeCleaner\Fixtures\ClassWithCallStatic::doStuff()'],
['Psy\Test\CodeCleaner\Fixtures\TraitWithStatic::doStuff()'],

// Allow `self` and `static` as class names.
['
Expand Down

0 comments on commit ed3c32c

Please sign in to comment.