Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Start working on NS Manager aka AliasManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jul 25, 2015
1 parent 0f2848f commit 902dae8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
6 changes: 5 additions & 1 deletion sandbox/Test.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace Sandbox;

use stdClass;

/**
* Class Test
*/
Expand All @@ -10,6 +14,6 @@ class Test
*/
public function returnTrue()
{
return 11 % 10;
return new stdClass();
}
}
47 changes: 47 additions & 0 deletions src/AliasManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @author Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/

namespace PHPSA;

class AliasManager
{
/**
* Current namespace, but this can be null
* @var string|null
*/
protected $namespace = null;

public function __construct($namespace)
{
$this->namespace = $namespace;
}

/**
* @var string[]
*/
protected $aliases = array();

public function isClassImported($classNS)
{
if (isset($this->aliases[$classNS])) {
return true;
}

return false;
}

public function add($ns)
{
$this->aliases[] = $ns;
}

/**
* @return null|string
*/
public function getNamespace()
{
return $this->namespace;
}
}
13 changes: 12 additions & 1 deletion src/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

namespace PHPSA\Command;

use PHPSA\AliasManager;
use PHPSA\Application;
use PHPSA\CompiledExpression;
use PHPSA\Compiler;
use PHPSA\Context;
use PHPSA\Definition\ClassDefinition;
use PHPSA\Definition\ClassMethod;
use PHPSA\Definition\FunctionDefinition;
use PHPSA\Visitor\Expression;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RuntimeException;
Expand Down Expand Up @@ -158,8 +161,16 @@ protected function parserFile($filepath, Parser $parser, Context $context)
$stmts = $stmts[0]->stmts;
}

$aliasManager = new AliasManager($namespace);

foreach ($stmts as $statement) {
if ($statement instanceof Node\Stmt\Class_) {
if ($statement instanceof Node\Stmt\Use_) {
if (!empty($statement->uses)) {
foreach ($statement->uses as $use) {
$aliasManager->add($use->name->parts);
}
}
} elseif ($statement instanceof Node\Stmt\Class_) {
$definition = new ClassDefinition($statement->name);
$definition->setFilepath($filepath);

Expand Down
5 changes: 5 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Context
*/
public $scope;

/**
* @var AliasManager
*/
public $aliasManager;

/**
* @var Application
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Visitor/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,11 @@ protected function passNew(Node\Expr\New_ $expr)
return new CompiledExpression(CompiledExpression::OBJECT);
}

if (class_exists($name, true)) {
return new CompiledExpression(CompiledExpression::OBJECT, new $name());
}

return new CompiledExpression(CompiledExpression::OBJECT);
// return new CompiledExpression(CompiledExpression::OBJECT, new $name());
}

$this->context->debug('Unknown how to pass new');
Expand Down

0 comments on commit 902dae8

Please sign in to comment.