Skip to content

Commit

Permalink
Refs #88, Refs #141: full-qualified-class attribute …
Browse files Browse the repository at this point in the history
… added to summary report.
  • Loading branch information
Manuel Pichler committed Jan 11, 2017
1 parent 3367b7a commit 13e9cbc
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/main/php/PDepend/Report/Summary/Xml.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ private function generateTypeXml(ASTClass $type, $typeIdentifier)


$typeXml = $doc->createElement($typeIdentifier); $typeXml = $doc->createElement($typeIdentifier);
$typeXml->setAttribute('name', Utf8Util::ensureEncoding($type->getName())); $typeXml->setAttribute('name', Utf8Util::ensureEncoding($type->getName()));
$typeXml->setAttribute('fqname', Utf8Util::ensureEncoding($type->getNamespacedName()));
$typeXml->setAttribute('start', Utf8Util::ensureEncoding($type->getStartLine())); $typeXml->setAttribute('start', Utf8Util::ensureEncoding($type->getStartLine()));
$typeXml->setAttribute('end', Utf8Util::ensureEncoding($type->getEndLine())); $typeXml->setAttribute('end', Utf8Util::ensureEncoding($type->getEndLine()));


Expand Down
21 changes: 21 additions & 0 deletions src/main/php/PDepend/Source/AST/ASTNamespace.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class ASTNamespace extends AbstractASTArtifact
*/ */
private $userDefined = null; private $userDefined = null;


/**
* @var boolean
*/
protected $packageAnnotation = false;

/** /**
* Constructs a new namespace for the given <b>$name</b> * Constructs a new namespace for the given <b>$name</b>
* *
Expand Down Expand Up @@ -288,6 +293,22 @@ public function removeFunction(ASTFunction $function)
} }
} }


/**
* @return boolean
*/
public function isPackageAnnotation()
{
return $this->packageAnnotation;
}

/**
* @param boolean $packageAnnotation
*/
public function setPackageAnnotation($packageAnnotation)
{
$this->packageAnnotation = $packageAnnotation;
}

/** /**
* ASTVisitor method for node tree traversal. * ASTVisitor method for node tree traversal.
* *
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/PDepend/Source/AST/AbstractASTType.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public function getTokens()
/** /**
* Sets the tokens for this type. * Sets the tokens for this type.
* *
* @param \PDepend\Source\Tokenizer\Token[] $tokens The generated tokens. * @param \PDepend\Source\Tokenizer\Token[] $tokens
* @return void * @return void
*/ */
public function setTokens(array $tokens) public function setTokens(array $tokens)
Expand All @@ -386,7 +386,7 @@ public function setTokens(array $tokens)
*/ */
public function getNamespacedName() public function getNamespacedName()
{ {
if (null === $this->namespaceName) { if (null === $this->namespace || $this->namespace->isPackageAnnotation()) {
return $this->name; return $this->name;
} }
return sprintf('%s\\%s', $this->namespaceName, $this->name); return sprintf('%s\\%s', $this->namespaceName, $this->name);
Expand Down
17 changes: 10 additions & 7 deletions src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1164,16 +1164,16 @@ private function parseFunctionDeclaration()


// First check for an existing namespace // First check for an existing namespace
if ($this->namespaceName !== null) { if ($this->namespaceName !== null) {
$packageName = $this->namespaceName; $namespaceName = $this->namespaceName;
} elseif ($this->packageName !== Builder::DEFAULT_NAMESPACE) { } elseif ($this->packageName !== Builder::DEFAULT_NAMESPACE) {
$packageName = $this->packageName; $namespaceName = $this->packageName;
} else { } else {
$packageName = $this->globalPackageName; $namespaceName = $this->globalPackageName;
} }


$this->builder $namespace = $this->builder->buildNamespace($namespaceName);
->buildNamespace($packageName) $namespace->setPackageAnnotation(null === $this->namespaceName);
->addFunction($function); $namespace->addFunction($function);


// Store function in source file, because we need them during the file's // Store function in source file, because we need them during the file's
// __wakeup() phase for function declarations within another function or // __wakeup() phase for function declarations within another function or
Expand Down Expand Up @@ -6570,7 +6570,10 @@ private function getNamespaceOrPackageName()
*/ */
private function getNamespaceOrPackage() private function getNamespaceOrPackage()
{ {
return $this->builder->buildNamespace($this->getNamespaceOrPackageName()); $namespace = $this->builder->buildNamespace($this->getNamespaceOrPackageName());
$namespace->setPackageAnnotation(null === $this->namespaceName);

return $namespace;
} }


/** /**
Expand Down
28 changes: 21 additions & 7 deletions src/test/php/PDepend/Report/Summary/XmlTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -246,13 +246,14 @@ public function testAnalyzersThatImplementProjectAndNodeAwareAsExpected()
} }


/** /**
* testNodeAwareAnalyzer * @param string $fixture
* * @param string $expectation
* @return void * @return void
* @dataProvider dataProviderNodeAware
*/ */
public function testNodeAwareAnalyzer() public function testNodeAwareAnalyzer($fixture, $expectation)
{ {
$this->namespaces = $this->parseCodeResourceForTest(); $this->namespaces = $this->parseSource($fixture);


$input = array( $input = array(
array('loc' => 42), array('ncloc' => 23), array('loc' => 42), array('ncloc' => 23),
Expand Down Expand Up @@ -298,19 +299,32 @@ public function testNodeAwareAnalyzer()


$log->close(); $log->close();


$fileName = 'node-aware-result-set.xml';
$this->assertXmlStringEqualsXmlString( $this->assertXmlStringEqualsXmlString(
$this->getNormalizedPathXml(dirname(__FILE__) . "/_expected/{$fileName}"), $this->getNormalizedPathXml(dirname(__FILE__) . "/_expected/{$expectation}"),
$this->getNormalizedPathXml($this->resultFile) $this->getNormalizedPathXml($this->resultFile)
); );
} }


public function dataProviderNodeAware()
{
return array(
array(
'Report/Summary/Xml/testNodeAwareAnalyzerWithNamespaces.php',
'node-aware-result-set-with-namespaces.xml',
),
array(
'Report/Summary/Xml/testNodeAwareAnalyzerWithPackages.php',
'node-aware-result-set-with-packages.xml',
),
);
}

protected function getNormalizedPathXml($fileName) protected function getNormalizedPathXml($fileName)
{ {
return preg_replace( return preg_replace(
array('(file\s+name="[^"]+")', '(generated="[^"]*")'), array('(file\s+name="[^"]+")', '(generated="[^"]*")'),
array('file name="' . __FILE__ . '"', 'generated=""'), array('file name="' . __FILE__ . '"', 'generated=""'),
file_get_contents($fileName) file_get_contents($fileName)
); );
} }
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</function> </function>
</package> </package>
<package name="pkg1" baz="23" foobar="42"> <package name="pkg1" baz="23" foobar="42">
<class name="FooBar" start="30" end="49" baz="23" foobar="42"> <class fqname="FooBar" name="FooBar" start="30" end="49" baz="23" foobar="42">
<file name="???"/> <file name="???"/>
<method name="x" start="43" end="43" baz="23" foobar="42"/> <method name="x" start="43" end="43" baz="23" foobar="42"/>
<method name="y" start="44" end="48" baz="23" foobar="42"/> <method name="y" start="44" end="48" baz="23" foobar="42"/>
Expand All @@ -19,7 +19,7 @@
</function> </function>
</package> </package>
<package name="pkg3" baz="23" foobar="42"> <package name="pkg3" baz="23" foobar="42">
<class name="Bar" start="23" end="25" baz="23" foobar="42"> <class fqname="Bar" name="Bar" start="23" end="25" baz="23" foobar="42">
<file name="???"/> <file name="???"/>
<method name="y" start="24" end="24" baz="23" foobar="42"/> <method name="y" start="24" end="24" baz="23" foobar="42"/>
</class> </class>
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<metrics generated="2015-09-23T06:23:41" pdepend="@package_version@">
<files>
<file name="???"/>
</files>
<package name="pkg1" loc="42" ncloc="23">
<class fqname="pkg1\FooBar" name="FooBar" start="10" end="29" loc="9" ncloc="7">
<file name="???"/>
<method name="x" start="23" end="23" loc="101" ncloc="99"/>
<method name="y" start="24" end="28" loc="90" ncloc="80"/>
</class>
<function name="foo" start="4" end="8" loc="50" ncloc="45">
<file name="???"/>
</function>
</package>
<package name="pkg3" loc="9" ncloc="9">
<class fqname="pkg3\Bar" name="Bar" start="41" end="45" loc="3" ncloc="3">
<file name="???"/>
<method name="y" start="44" end="44" loc="42" ncloc="23"/>
</class>
</package>
<package name="" loc="33" ncloc="20">
<function name="bar" start="49" end="51" loc="9" ncloc="7">
<file name="???"/>
</function>
</package>
</metrics>
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</function> </function>
</package> </package>
<package name="pkg1" loc="101" ncloc="99"> <package name="pkg1" loc="101" ncloc="99">
<class name="FooBar" start="30" end="49" loc="90" ncloc="80"> <class fqname="FooBar" name="FooBar" start="30" end="49" loc="90" ncloc="80">
<file name="???"/> <file name="???"/>
<method name="x" start="43" end="43" loc="50" ncloc="45"/> <method name="x" start="43" end="43" loc="50" ncloc="45"/>
<method name="y" start="44" end="48" loc="30" ncloc="22"/> <method name="y" start="44" end="48" loc="30" ncloc="22"/>
Expand All @@ -19,7 +19,7 @@
</function> </function>
</package> </package>
<package name="pkg3" loc="42" ncloc="23"> <package name="pkg3" loc="42" ncloc="23">
<class name="Bar" start="23" end="25" loc="33" ncloc="20"> <class fqname="Bar" name="Bar" start="23" end="25" loc="33" ncloc="20">
<file name="???"/> <file name="???"/>
<method name="y" start="24" end="24" loc="9" ncloc="7"/> <method name="y" start="24" end="24" loc="9" ncloc="7"/>
</class> </class>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</function> </function>
</package> </package>
<package name="pkg1"> <package name="pkg1">
<class name="FooBar" start="30" end="49"> <class fqname="FooBar" name="FooBar" start="30" end="49">
<file name="???"/> <file name="???"/>
<method name="x" start="43" end="43"/> <method name="x" start="43" end="43"/>
<method name="y" start="44" end="48"/> <method name="y" start="44" end="48"/>
Expand All @@ -19,7 +19,7 @@
</function> </function>
</package> </package>
<package name="pkg3"> <package name="pkg3">
<class name="Bar" start="23" end="25"> <class fqname="Bar" name="Bar" start="23" end="25">
<file name="???"/> <file name="???"/>
<method name="y" start="24" end="24"/> <method name="y" start="24" end="24"/>
</class> </class>
Expand Down
34 changes: 34 additions & 0 deletions src/test/php/PDepend/Source/AST/ASTClassTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1563,6 +1563,40 @@ public function testIsCachedReturnsFalseWhenObjectGetsSerialized()
$this->assertFalse($class->isCached()); $this->assertFalse($class->isCached());
} }


/**
* @return void
*/
public function testGetNamespacedName()
{
$class = new ASTClass('MyClass');
$this->assertSame('MyClass', $class->getNamespacedName());
}

/**
* @return void
*/
public function testGetNamespacedNameWithNamespaceDeclaration()
{
$class = new ASTClass('MyClass');
$class->setNamespace(new ASTNamespace('My\\Namespace'));

$this->assertSame('My\\Namespace\\MyClass', $class->getNamespacedName());
}

/**
* @return void
*/
public function testGetNamespacedNameWithPackageAnnotation()
{
$namespace = new ASTNamespace('My\\Namespace');
$namespace->setPackageAnnotation(true);

$class = new ASTClass('MyClass');
$class->setNamespace($namespace);

$this->assertSame('MyClass', $class->getNamespacedName());
}

/** /**
* testMagicSleepMethodReturnsExpectedSetOfPropertyNames * testMagicSleepMethodReturnsExpectedSetOfPropertyNames
* *
Expand Down
35 changes: 34 additions & 1 deletion src/test/php/PDepend/Source/AST/ASTInterfaceTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@


namespace PDepend\Source\AST; namespace PDepend\Source\AST;


use PDepend\Source\Builder\BuilderContext;
use PDepend\Source\Tokenizer\Token; use PDepend\Source\Tokenizer\Token;
use PDepend\Util\Cache\Driver\MemoryCacheDriver; use PDepend\Util\Cache\Driver\MemoryCacheDriver;


Expand Down Expand Up @@ -830,6 +829,40 @@ public function testIsCachedReturnsFalseWhenObjectGetsSerialized()
$this->assertFalse($interface->isCached()); $this->assertFalse($interface->isCached());
} }


/**
* @return void
*/
public function testGetNamespacedName()
{
$interface = new ASTInterface('MyInterface');
$this->assertSame('MyInterface', $interface->getNamespacedName());
}

/**
* @return void
*/
public function testGetNamespacedNameWithNamespaceDeclaration()
{
$interface = new ASTInterface('MyInterface');
$interface->setNamespace(new ASTNamespace('My\\Namespace'));

$this->assertSame('My\\Namespace\\MyInterface', $interface->getNamespacedName());
}

/**
* @return void
*/
public function testGetNamespacedNameWithPackageAnnotation()
{
$namespace = new ASTNamespace('My\\Namespace');
$namespace->setPackageAnnotation(true);

$interface = new ASTInterface('MyInterface');
$interface->setNamespace($namespace);

$this->assertSame('MyInterface', $interface->getNamespacedName());
}

/** /**
* testMagicSleepMethodReturnsExpectedSetOfPropertyNames * testMagicSleepMethodReturnsExpectedSetOfPropertyNames
* *
Expand Down
22 changes: 21 additions & 1 deletion src/test/php/PDepend/Source/AST/ASTNamespaceTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function testRemoveType()
public function testRemoveTypeResetsPackageReferenceFromRemovedType() public function testRemoveTypeResetsPackageReferenceFromRemovedType()
{ {
$namespace = new ASTNamespace('package1'); $namespace = new ASTNamespace('package1');
$class = new ASTClass('Class'); $class = new ASTClass('Class');


$namespace->addType($class); $namespace->addType($class);
$namespace->removeType($class); $namespace->removeType($class);
Expand Down Expand Up @@ -461,4 +461,24 @@ public function testIsUserDefinedReturnsTrueWhenAtLeastOneFunctionExists()


$this->assertTrue($namespace->isUserDefined()); $this->assertTrue($namespace->isUserDefined());
} }

/**
* @return void
*/
public function testIsPackageAnnotationReturnsFalseByDefault()
{
$namespace = new ASTNamespace('namespace');
$this->assertFalse($namespace->isPackageAnnotation());
}

/**
* @return void
*/
public function testIsPackageAnnotationReturnsFalseTrue()
{
$namespace = new ASTNamespace('namespace');
$namespace->setPackageAnnotation(true);

$this->assertTrue($namespace->isPackageAnnotation());
}
} }
34 changes: 34 additions & 0 deletions src/test/php/PDepend/Source/AST/ASTTraitTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -380,6 +380,40 @@ public function testTraitCanUseParentKeywordAsMethodTypeHint()
$this->assertNotNull($trait); $this->assertNotNull($trait);
} }


/**
* @return void
*/
public function testGetNamespacedName()
{
$trait = new ASTTrait('MyTrait');
$this->assertSame('MyTrait', $trait->getNamespacedName());
}

/**
* @return void
*/
public function testGetNamespacedNameWithNamespaceDeclaration()
{
$trait = new ASTTrait('MyTrait');
$trait->setNamespace(new ASTNamespace('My\\Namespace'));

$this->assertSame('My\\Namespace\\MyTrait', $trait->getNamespacedName());
}

/**
* @return void
*/
public function testGetNamespacedNameWithPackageAnnotation()
{
$namespace = new ASTNamespace('My\\Namespace');
$namespace->setPackageAnnotation(true);

$Trait = new ASTTrait('MyTrait');
$Trait->setNamespace($namespace);

$this->assertSame('MyTrait', $Trait->getNamespacedName());
}

/** /**
* testAcceptInvokesVisitTraitOnGivenVisitor * testAcceptInvokesVisitTraitOnGivenVisitor
* *
Expand Down
Loading

0 comments on commit 13e9cbc

Please sign in to comment.