/** * Visits a class node. * * @param \PDepend\Source\AST\ASTClass $class * @return void */ public function visitClass(ASTClass $class) { if (!$class->isUserDefined()) { return; } $doc = $this->packages->ownerDocument; $classXml = $doc->createElement('Class'); $classXml->setAttribute('sourceFile', (string) $class->getCompilationUnit()); $classXml->appendChild($doc->createTextNode($class->getName())); if ($class->isAbstract()) { $this->abstractClasses->appendChild($classXml); } else { $this->concreteClasses->appendChild($classXml); } }
/** * This method will persist a class instance for later reuse. * * @param string $className * @param string $namespaceName * @param \PDepend\Source\AST\ASTClass $class * @return void * @@since 0.9.5 */ protected function storeClass($className, $namespaceName, ASTClass $class) { $className = strtolower($className); if (!isset($this->classes[$className][$namespaceName])) { $this->classes[$className][$namespaceName] = array(); } $this->classes[$className][$namespaceName][$class->getId()] = $class; $namespace = $this->buildNamespace($namespaceName); $namespace->addType($class); }
/** * The magic wakeup method will be called by PHP's runtime environment when * a serialized instance of this class was unserialized. This implementation * of the wakeup method will register this object in the the global class * context. * * @return void */ public function __wakeup() { parent::__wakeup(); $this->context->registerTrait($this); }
/** * Visits a class node. * * @param \PDepend\Source\AST\ASTClass $class * @return void */ public function visitClass(ASTClass $class) { $this->visits[] = $class->getName(); parent::visitClass($class); }
/** * Creates a ready to use class fixture. * * @param string $name Optional class name. * * @return \PDepend\Source\AST\ASTClass * @since 1.0.2 */ protected function createClassFixture($name = null) { $name = $name ? $name : get_class($this); $class = new ASTClass($name); $class->setCompilationUnit(new ASTCompilationUnit($GLOBALS['argv'][0])); $class->setCache(new MemoryCacheDriver()); $class->setContext($this->getMock('PDepend\\Source\\Builder\\BuilderContext')); return $class; }
/** * Generates the XML for a class or trait node. * * @param \PDepend\Source\AST\ASTClass $type * @param string $typeIdentifier * @return void */ private function generateTypeXml(ASTClass $type, $typeIdentifier) { if (!$type->isUserDefined()) { return; } $xml = end($this->xmlStack); $doc = $xml->ownerDocument; $typeXml = $doc->createElement($typeIdentifier); $typeXml->setAttribute('name', $type->getName()); $this->writeNodeMetrics($typeXml, $type); $this->writeFileReference($typeXml, $type->getCompilationUnit()); $xml->appendChild($typeXml); array_push($this->xmlStack, $typeXml); foreach ($type->getMethods() as $method) { $method->accept($this); } foreach ($type->getProperties() as $property) { $property->accept($this); } array_pop($this->xmlStack); }
/** * Visits a class node. * * @param \PDepend\Source\AST\ASTClass $node * @return void */ public function visitClass(ASTClass $node) { if (!$node->isUserDefined()) { return; } $this->apply(new ClassNode($node)); parent::visitClass($node); }
/** * Calculates metrics for the given <b>$class</b> instance. * * @param \PDepend\Source\AST\ASTClass $class * @return void */ public function visitClass(ASTClass $class) { if (false === $class->isUserDefined()) { return; } $this->fireStartClass($class); ++$this->cls; if ($class->isAbstract()) { ++$this->clsa; } $parentClass = $class->getParentClass(); if ($parentClass !== null) { if ($parentClass->getParentClass() === null) { $this->roots[$parentClass->getId()] = true; } $this->noneLeafs[$parentClass->getId()] = true; } // Store node metric $this->nodeMetrics[$class->getId()] = array(); foreach ($class->getMethods() as $method) { $method->accept($this); } foreach ($class->getProperties() as $property) { $property->accept($this); } $this->fireEndClass($class); }
/** * Calculates the Variables Inheritance of a class metric, this method only * counts protected and public properties of parent classes. * * @param \PDepend\Source\AST\ASTClass $class The context class instance. * @return integer */ private function calculateVarsi(ASTClass $class) { // List of properties, this method only counts not overwritten properties $properties = array(); // Collect all properties of the context class foreach ($class->getProperties() as $prop) { $properties[$prop->getName()] = true; } foreach ($class->getParentClasses() as $parent) { foreach ($parent->getProperties() as $prop) { if (!$prop->isPrivate() && !isset($properties[$prop->getName()])) { $properties[$prop->getName()] = true; } } } return count($properties); }
/** * Tests that build interface updates the source file information for null * values. * * @return void */ public function testSetSourceFileInformationForNullValue() { $file = new ASTCompilationUnit(__FILE__); $class = new ASTClass(__CLASS__); $class->setCompilationUnit($file); $method = new ASTMethod(__FUNCTION__); $method->setParent($class); $this->assertSame($file, $method->getCompilationUnit()); }
/** * Visits a class node. * * @param \PDepend\Source\AST\ASTClass $class * @return void */ public function visitClass(ASTClass $class) { if (!$class->isUserDefined()) { return; } $xml = end($this->xmlStack); $doc = $xml->ownerDocument; $classXml = $doc->createElement('class'); $classXml->setAttribute('name', $class->getName()); $this->writeNodeMetrics($classXml, $class); $this->writeFileReference($classXml, $class->getCompilationUnit()); $xml->appendChild($classXml); array_push($this->xmlStack, $classXml); foreach ($class->getMethods() as $method) { $method->accept($this); } foreach ($class->getProperties() as $property) { $property->accept($this); } array_pop($this->xmlStack); }
/** * testIsUserDefinedReturnsTrueWhenChildElementIsUserDefined * * @return void */ public function testIsUserDefinedReturnsTrueWhenChildElementIsUserDefined() { $class = new ASTClass('class', 0); $class->setUserDefined(); $namespace = new ASTNamespace('package1'); $namespace->addType($class); $this->assertTrue($namespace->isUserDefined()); }
/** * testBuilderCreatesCaseInSensitiveMethodIdentifiers * * @return void */ public function testBuilderCreatesCaseInSensitiveMethodIdentifiers() { $compilationUnit = new ASTCompilationUnit(__FILE__); $compilationUnit->setId(__FUNCTION__); $class = new ASTClass(__FUNCTION__); $class->setCompilationUnit($compilationUnit); $method0 = new ASTMethod(__FUNCTION__); $method0->setParent($class); $method1 = new ASTMethod(strtolower(__FUNCTION__)); $method1->setParent($class); $builder0 = new IdBuilder(); $builder1 = new IdBuilder(); $this->assertEquals($builder0->forMethod($method0), $builder1->forMethod($method1)); }
/** * Parses a parent class declaration for the given <b>$class</b>. * * @param \PDepend\Source\AST\ASTClass $class * @return \PDepend\Source\AST\ASTClass * @since 1.0.0 */ private function parseClassExtends(ASTClass $class) { $this->consumeToken(Tokens::T_EXTENDS); $this->tokenStack->push(); $class->setParentClassReference($this->setNodePositionsAndReturn($this->builder->buildAstClassReference($this->parseQualifiedName()))); return $class; }
/** * The magic wakeup method will be called by PHP's runtime environment when * a serialized instance of this class was unserialized. This implementation * of the wakeup method will register this object in the the global class * context. * * @return void */ public function __wakeup() { $this->methods = null; foreach ($this->nodes as $node) { $node->setParent($this); } parent::__wakeup(); }
/** * Visits a class node. * * @param \PDepend\Source\AST\ASTClass $class * @return void */ public function visitClass(ASTClass $class) { if (false === $class->isUserDefined()) { return; } $this->fireStartClass($class); // Update global class count ++$this->noc; $id = $class->getNamespace()->getId(); ++$this->nodeMetrics[$id][self::M_NUMBER_OF_CLASSES]; $this->nodeMetrics[$class->getId()] = array(self::M_NUMBER_OF_METHODS => 0); foreach ($class->getMethods() as $method) { $method->accept($this); } $this->fireEndClass($class); }
/** * Initializes a empty metric container for the given class node. * * @param \PDepend\Source\AST\ASTClass $class * @return void * @since 0.9.10 */ private function initNodeMetricsForClass(ASTClass $class) { $id = $class->getId(); if (isset($this->nodeMetrics[$id])) { return; } ++$this->numberOfClasses; $this->nodeMetrics[$id] = array(self::M_DEPTH_OF_INHERITANCE_TREE => 0, self::M_NUMBER_OF_ADDED_METHODS => 0, self::M_NUMBER_OF_DERIVED_CLASSES => 0, self::M_NUMBER_OF_OVERWRITTEN_METHODS => 0); foreach ($class->getParentClasses() as $parent) { $this->initNodeMetricsForClass($parent); } }
/** * Creates an abstract item instance. * * @return \PDepend\Source\AST\AbstractASTArtifact */ protected function createItem() { $class = new ASTClass(__CLASS__); $class->setCompilationUnit(new ASTCompilationUnit(__FILE__)); $class->setCache(new MemoryCacheDriver()); $class->setContext($this->getMock('PDepend\\Source\\Builder\\BuilderContext')); return $class; }
/** * testIteratorReturnsSameClassOnlyOnce * * @return void */ public function testIteratorReturnsSameClassOnlyOnce() { $class1 = new ASTClass('c1'); $class1->setId(md5(23)); $class2 = new ASTClass('c2'); $class2->setId(md5(23)); $reference1 = $this->getMockBuilder('PDepend\\Source\\AST\\ASTSelfReference')->disableOriginalConstructor()->getMock(); $reference1->expects($this->once())->method('getType')->will($this->returnValue($class1)); $reference2 = $this->getMockBuilder('PDepend\\Source\\AST\\ASTSelfReference')->disableOriginalConstructor()->getMock(); $reference2->expects($this->once())->method('getType')->will($this->returnValue($class2)); $references = array($reference1, $reference2); $refs = new ASTClassOrInterfaceReferenceIterator($references); $types = array(); foreach ($refs as $type) { $types[] = $type->getId(); } $this->assertEquals(array($class1->getId()), $types); }