Permalink
Browse files

Refs #88, Refs #141: full-qualified-class attribute …

… added to summary report.
  • Loading branch information...
1 parent 3367b7a commit 13e9cbc8716a1850f789728aac66583b2806c89d @manuelpichler manuelpichler committed Jan 11, 2017
@@ -293,6 +293,7 @@ private function generateTypeXml(ASTClass $type, $typeIdentifier)
$typeXml = $doc->createElement($typeIdentifier);
$typeXml->setAttribute('name', Utf8Util::ensureEncoding($type->getName()));
+ $typeXml->setAttribute('fqname', Utf8Util::ensureEncoding($type->getNamespacedName()));
$typeXml->setAttribute('start', Utf8Util::ensureEncoding($type->getStartLine()));
$typeXml->setAttribute('end', Utf8Util::ensureEncoding($type->getEndLine()));
@@ -83,6 +83,11 @@ class ASTNamespace extends AbstractASTArtifact
private $userDefined = null;
/**
+ * @var boolean
+ */
+ protected $packageAnnotation = false;
+
+ /**
* Constructs a new namespace for the given <b>$name</b>
*
* @param string $name
@@ -289,6 +294,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.
*
* @param \PDepend\Source\ASTVisitor\ASTVisitor $visitor
@@ -368,7 +368,7 @@ public function getTokens()
/**
* Sets the tokens for this type.
*
- * @param \PDepend\Source\Tokenizer\Token[] $tokens The generated tokens.
+ * @param \PDepend\Source\Tokenizer\Token[] $tokens
* @return void
*/
public function setTokens(array $tokens)
@@ -386,7 +386,7 @@ public function setTokens(array $tokens)
*/
public function getNamespacedName()
{
- if (null === $this->namespaceName) {
+ if (null === $this->namespace || $this->namespace->isPackageAnnotation()) {
return $this->name;
}
return sprintf('%s\\%s', $this->namespaceName, $this->name);
@@ -1164,16 +1164,16 @@ private function parseFunctionDeclaration()
// First check for an existing namespace
if ($this->namespaceName !== null) {
- $packageName = $this->namespaceName;
+ $namespaceName = $this->namespaceName;
} elseif ($this->packageName !== Builder::DEFAULT_NAMESPACE) {
- $packageName = $this->packageName;
+ $namespaceName = $this->packageName;
} else {
- $packageName = $this->globalPackageName;
+ $namespaceName = $this->globalPackageName;
}
- $this->builder
- ->buildNamespace($packageName)
- ->addFunction($function);
+ $namespace = $this->builder->buildNamespace($namespaceName);
+ $namespace->setPackageAnnotation(null === $this->namespaceName);
+ $namespace->addFunction($function);
// Store function in source file, because we need them during the file's
// __wakeup() phase for function declarations within another function or
@@ -6570,7 +6570,10 @@ private function getNamespaceOrPackageName()
*/
private function getNamespaceOrPackage()
{
- return $this->builder->buildNamespace($this->getNamespaceOrPackageName());
+ $namespace = $this->builder->buildNamespace($this->getNamespaceOrPackageName());
+ $namespace->setPackageAnnotation(null === $this->namespaceName);
+
+ return $namespace;
}
/**
@@ -246,13 +246,14 @@ public function testAnalyzersThatImplementProjectAndNodeAwareAsExpected()
}
/**
- * testNodeAwareAnalyzer
- *
+ * @param string $fixture
+ * @param string $expectation
* @return void
+ * @dataProvider dataProviderNodeAware
*/
- public function testNodeAwareAnalyzer()
+ public function testNodeAwareAnalyzer($fixture, $expectation)
{
- $this->namespaces = $this->parseCodeResourceForTest();
+ $this->namespaces = $this->parseSource($fixture);
$input = array(
array('loc' => 42), array('ncloc' => 23),
@@ -298,19 +299,32 @@ public function testNodeAwareAnalyzer()
$log->close();
- $fileName = 'node-aware-result-set.xml';
$this->assertXmlStringEqualsXmlString(
- $this->getNormalizedPathXml(dirname(__FILE__) . "/_expected/{$fileName}"),
+ $this->getNormalizedPathXml(dirname(__FILE__) . "/_expected/{$expectation}"),
$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)
{
return preg_replace(
array('(file\s+name="[^"]+")', '(generated="[^"]*")'),
array('file name="' . __FILE__ . '"', 'generated=""'),
- file_get_contents($fileName)
+ file_get_contents($fileName)
);
}
}
@@ -9,7 +9,7 @@
</function>
</package>
<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="???"/>
<method name="x" start="43" end="43" baz="23" foobar="42"/>
<method name="y" start="44" end="48" baz="23" foobar="42"/>
@@ -19,7 +19,7 @@
</function>
</package>
<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="???"/>
<method name="y" start="24" end="24" baz="23" foobar="42"/>
</class>
@@ -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>
@@ -9,7 +9,7 @@
</function>
</package>
<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="???"/>
<method name="x" start="43" end="43" loc="50" ncloc="45"/>
<method name="y" start="44" end="48" loc="30" ncloc="22"/>
@@ -19,7 +19,7 @@
</function>
</package>
<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="???"/>
<method name="y" start="24" end="24" loc="9" ncloc="7"/>
</class>
@@ -9,7 +9,7 @@
</function>
</package>
<package name="pkg1">
- <class name="FooBar" start="30" end="49">
+ <class fqname="FooBar" name="FooBar" start="30" end="49">
<file name="???"/>
<method name="x" start="43" end="43"/>
<method name="y" start="44" end="48"/>
@@ -19,7 +19,7 @@
</function>
</package>
<package name="pkg3">
- <class name="Bar" start="23" end="25">
+ <class fqname="Bar" name="Bar" start="23" end="25">
<file name="???"/>
<method name="y" start="24" end="24"/>
</class>
@@ -1564,6 +1564,40 @@ public function testIsCachedReturnsFalseWhenObjectGetsSerialized()
}
/**
+ * @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
*
* @return void
@@ -42,7 +42,6 @@
namespace PDepend\Source\AST;
-use PDepend\Source\Builder\BuilderContext;
use PDepend\Source\Tokenizer\Token;
use PDepend\Util\Cache\Driver\MemoryCacheDriver;
@@ -831,6 +830,40 @@ public function testIsCachedReturnsFalseWhenObjectGetsSerialized()
}
/**
+ * @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
*
* @return void
@@ -226,7 +226,7 @@ public function testRemoveType()
public function testRemoveTypeResetsPackageReferenceFromRemovedType()
{
$namespace = new ASTNamespace('package1');
- $class = new ASTClass('Class');
+ $class = new ASTClass('Class');
$namespace->addType($class);
$namespace->removeType($class);
@@ -461,4 +461,24 @@ public function testIsUserDefinedReturnsTrueWhenAtLeastOneFunctionExists()
$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());
+ }
}
@@ -381,6 +381,40 @@ public function testTraitCanUseParentKeywordAsMethodTypeHint()
}
/**
+ * @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
*
* @return void
Oops, something went wrong.

0 comments on commit 13e9cbc

Please sign in to comment.