Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
28 / 28 |
PhpFileBuilder | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
28 / 28 |
__construct($absPath) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getNode() | |
100.00% |
1 / 1 |
4 | |
100.00% |
12 / 12 |
|||
declaring($stmt) | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
ns($str) | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
addUse($str) | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
<?php | |
/* | |
* Mondrian | |
*/ | |
namespace Trismegiste\Mondrian\Parser; | |
/** | |
* PhpFileBuilder is a builder for a PhpFile node : | |
* Enforces the PSR-0 : one class per file | |
*/ | |
class PhpFileBuilder extends \PHPParser_BuilderAbstract | |
{ | |
protected $filename; | |
protected $fileNamespace = false; | |
protected $theClass = null; | |
protected $useList = array(); | |
public function __construct($absPath) | |
{ | |
$this->filename = $absPath; | |
} | |
public function getNode() | |
{ | |
$stmts = array(); | |
if ($this->fileNamespace) { | |
$stmts[] = $this->fileNamespace; | |
} | |
if (count($this->useList)) { | |
$stmts = array_merge($stmts, $this->useList); | |
} | |
if (!is_null($this->theClass)) { | |
$stmts[] = $this->theClass; | |
} | |
return new PhpFile($this->filename, $stmts); | |
} | |
/** | |
* Declares a class or an interface | |
* | |
* @param mixed $stmt a Node_Stmt or a Node_Builder | |
* | |
* @return \Trismegiste\Mondrian\Parser\PhpFileBuilder this instance | |
* | |
* @throws \InvalidArgumentException | |
*/ | |
public function declaring($stmt) | |
{ | |
$node = $this->normalizeNode($stmt); | |
if (in_array($node->getType(), array('Stmt_Class', 'Stmt_Interface'))) { | |
$this->theClass = $node; | |
} else { | |
throw new \InvalidArgumentException("Invalid node expected type " . $node->getType()); | |
} | |
return $this; | |
} | |
/** | |
* Namespace | |
* | |
* @param string $str | |
* | |
* @return \Trismegiste\Mondrian\Parser\PhpFileBuilder this instance | |
*/ | |
public function ns($str) | |
{ | |
$this->fileNamespace = new \PHPParser_Node_Stmt_Namespace( | |
new \PHPParser_Node_Name((string) $str)); | |
return $this; | |
} | |
/** | |
* Add an "use fqcn" | |
* | |
* @param string $str | |
* | |
* @return \Trismegiste\Mondrian\Parser\PhpFileBuilder this instance | |
*/ | |
public function addUse($str) | |
{ | |
$this->useList[] = new \PHPParser_Node_Stmt_Use( | |
array( | |
new \PHPParser_Node_Stmt_UseUse( | |
new \PHPParser_Node_Name( | |
(string) $str)))); | |
return $this; | |
} | |
} |