Skip to content

Commit

Permalink
Simplify a lot of APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 7, 2017
1 parent 6ee69e5 commit 694da2c
Show file tree
Hide file tree
Showing 31 changed files with 820 additions and 943 deletions.
121 changes: 121 additions & 0 deletions src/Psalm/Checker/CanAlias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
namespace Psalm\Checker;

use PhpParser\Node\Stmt\Namespace_;
use PhpParser;
use Psalm\Context;
use Psalm\StatementsSource;
use Psalm\Type;

trait CanAlias
{
/**
* @var array<string, string>
*/
protected $aliased_classes = [];

/**
* @var array<string, string>
*/
protected $aliased_classes_flipped = [];

/**
* @var array<string, string>
*/
protected $aliased_functions = [];

/**
* @var array<string, string>
*/
protected $aliased_constants = [];

/**
* @param PhpParser\Node\Stmt\Use_ $stmt
* @return void
*/
public function visitUse(PhpParser\Node\Stmt\Use_ $stmt)
{
foreach ($stmt->uses as $use) {
$use_path = implode('\\', $use->name->parts);

switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
$this->aliased_functions[strtolower($use->alias)] = $use_path;
break;

case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
$this->aliased_constants[$use->alias] = $use_path;
break;

case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
$this->aliased_classes[strtolower($use->alias)] = $use_path;
$this->aliased_classes_flipped[strtolower($use_path)] = $use->alias;
break;
}
}
}

/**
* @param PhpParser\Node\Stmt\GroupUse $stmt
* @return void
*/
public function visitGroupUse(PhpParser\Node\Stmt\GroupUse $stmt)
{
$use_prefix = implode('\\', $stmt->prefix->parts);

foreach ($stmt->uses as $use) {
$use_path = $use_prefix . '\\' . implode('\\', $use->name->parts);

switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
$this->aliased_functions[strtolower($use->alias)] = $use_path;
break;

case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
$this->aliased_constants[$use->alias] = $use_path;
break;

case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
$this->aliased_classes[strtolower($use->alias)] = $use_path;
$this->aliased_classes_flipped[strtolower($use_path)] = $use->alias;
break;
}
}
}

/**
* @return array<string, string>
*/
public function getAliasedClasses()
{
return $this->aliased_classes;
}

/**
* @return array<string, string>
*/
public function getAliasedClassesFlipped()
{
return $this->aliased_classes_flipped;
}

/**
* Gets a list of all aliased constants
*
* @return array<string, string>
*/
public function getAliasedConstants()
{
return $this->aliased_constants;
}

/**
* Gets a list of all aliased functions
*
* @return array<string, string>
*/
public function getAliasedFunctions()
{
return $this->aliased_functions;
}
}
8 changes: 3 additions & 5 deletions src/Psalm/Checker/ClassChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ public function __construct(PhpParser\Node\Stmt\ClassLike $class, StatementsSour
self::$class_extends[$this->fq_class_name] = [];

if ($this->class->extends) {
$this->parent_class = self::getFQCLNFromNameObject(
$this->parent_fq_class_name = self::getFQCLNFromNameObject(
$this->class->extends,
$this->namespace,
$this->aliased_classes
$this->source
);
}

foreach ($class->implements as $interface_name) {
$fq_interface_name = self::getFQCLNFromNameObject(
$interface_name,
$this->namespace,
$this->aliased_classes
$this->source
);

$storage->class_implements[strtolower($fq_interface_name)] = $fq_interface_name;
Expand Down
Loading

0 comments on commit 694da2c

Please sign in to comment.