/**
  * Creates a Descriptor from the provided data.
  *
  * @param ConstantReflector $data
  *
  * @return ConstantDescriptor
  */
 public function create($data)
 {
     $constantDescriptor = new ConstantDescriptor();
     $constantDescriptor->setName($data->getShortName());
     $constantDescriptor->setValue($data->getValue());
     // Reflection library formulates namespace as global but this is not wanted for phpDocumentor itself
     $constantDescriptor->setNamespace('\\' . (strtolower($data->getNamespace()) == 'global' ? '' : $data->getNamespace()));
     $constantDescriptor->setFullyQualifiedStructuralElementName((trim($constantDescriptor->getNamespace(), '\\') ? $constantDescriptor->getNamespace() : '') . '\\' . $data->getShortName());
     $this->assembleDocBlock($data->getDocBlock(), $constantDescriptor);
     $constantDescriptor->setLine($data->getLinenumber());
     return $constantDescriptor;
 }
 /**
  * @param string $name The name of the current constant.
  *
  * @return ConstantDescriptor
  */
 protected function whenFixtureHasConstantInParentClassWithSameName($name)
 {
     $result = new ConstantDescriptor();
     $result->setName($name);
     $parent = new ClassDescriptor();
     $parent->getConstants()->set($name, $result);
     $class = new ClassDescriptor();
     $class->setParent($parent);
     $this->fixture->setParent($class);
     return $result;
 }
 /**
  * Export the given reflected constant definition to the provided parent element.
  *
  * @param \DOMElement        $parent Element to augment.
  * @param ConstantDescriptor $constant Element to export.
  *
  * @return \DOMElement
  */
 public function convert(\DOMElement $parent, ConstantDescriptor $constant)
 {
     $fullyQualifiedNamespaceName = $constant->getNamespace() instanceof NamespaceDescriptor ? $constant->getNamespace()->getFullyQualifiedStructuralElementName() : $parent->getAttribute('namespace');
     $child = new \DOMElement('constant');
     $parent->appendChild($child);
     $child->setAttribute('namespace', ltrim($fullyQualifiedNamespaceName, '\\'));
     $child->setAttribute('line', $constant->getLine());
     $child->appendChild(new \DOMElement('name', $constant->getName()));
     $child->appendChild(new \DOMElement('full_name', $constant->getFullyQualifiedStructuralElementName()));
     $child->appendChild(new \DOMElement('value'))->appendChild(new \DOMText($constant->getValue()));
     $this->docBlockConverter->convert($child, $constant);
     return $child;
 }
 /**
  * Generates a URL from the given node or returns false if unable.
  *
  * @param \phpDocumentor\Descriptor\ConstantDescriptor $node
  *
  * @return string|false
  */
 public function __invoke($node)
 {
     $name = $node->getName();
     // global constant
     if ($node->getParent() instanceof \phpDocumentor\Descriptor\FileDescriptor || !$node->getParent()) {
         $namespaceName = $node->getNamespace();
         return '/namespaces/' . str_replace('\\', '.', ltrim($namespaceName, '\\')) . '.html#constant_' . $name;
     }
     // class constant
     $className = $node->getParent()->getFullyQualifiedStructuralElementName();
     return '/classes/' . str_replace('\\', '.', ltrim($className, '\\')) . '.html#constant_' . $name;
 }
Beispiel #5
0
 /**
  * Exports the given constant to the parent XML element.
  *
  * This method creates a new child element on the given parent XML element
  * and takes the properties of the Reflection argument and sets the
  * elements and attributes on the child.
  *
  * If a child DOMElement is provided then the properties and attributes are
  * set on this but the child element is not appended onto the parent. This
  * is the responsibility of the invoker. Essentially this means that the
  * $parent argument is ignored in this case.
  *
  * @param \DOMElement        $parent   The parent element to augment.
  * @param ConstantDescriptor $constant The data source.
  * @param \DOMElement        $child    Optional: child element to use instead of creating a new one on the $parent.
  *
  * @return void
  */
 public function buildConstant(\DOMElement $parent, ConstantDescriptor $constant, \DOMElement $child = null)
 {
     if (!$constant->getName()) {
         return;
     }
     if (!$child) {
         $child = new \DOMElement('constant');
         $parent->appendChild($child);
     }
     $namespace = $constant->getNamespace() ? $constant->getNamespace() : $parent->getAttribute('namespace');
     $child->setAttribute('namespace', ltrim($namespace, '\\'));
     $child->setAttribute('line', $constant->getLine());
     $child->appendChild(new \DOMElement('name', $constant->getName()));
     $child->appendChild(new \DOMElement('full_name', $constant->getFullyQualifiedStructuralElementName()));
     $child->appendChild(new \DOMElement('value'))->appendChild(new \DOMText($constant->getValue()));
     $this->buildDocBlock($child, $constant);
 }
 /**
  * Returns the first part of the URL path that is specific to class constants.
  *
  * @param Descriptor\ConstantDescriptor $node
  *
  * @return string
  */
 private function getUrlPathPrefixForClassConstants($node)
 {
     return '/classes/' . $this->converter->fromClass($node->getParent()->getFullyQualifiedStructuralElementName());
 }
 /**
  * @covers phpDocumentor\Descriptor\InterfaceDescriptor::getInheritedConstants
  */
 public function testGetInheritedConstantsWithClassDescriptorParent()
 {
     $parentDescriptor = new ConstantDescriptor();
     $parentDescriptor->setName('parent');
     $parentDescriptorCollection = new Collection();
     $parentDescriptorCollection->add($parentDescriptor);
     $parent = new InterfaceDescriptor();
     $parent->setConstants($parentDescriptorCollection);
     $grandParentDescriptor = new ConstantDescriptor();
     $grandParentDescriptor->setName('grandparent');
     $grandParentDescriptorCollection = new Collection();
     $grandParentDescriptorCollection->add($grandParentDescriptor);
     $grandParent = new InterfaceDescriptor();
     $grandParent->setConstants($grandParentDescriptorCollection);
     $grandParentCollection = new Collection();
     $grandParentCollection->add($grandParent);
     $parent->setParent($grandParentCollection);
     $parentCollection = new Collection();
     $parentCollection->add($parent);
     $this->fixture->setParent($parentCollection);
     $result = $this->fixture->getInheritedConstants();
     $this->assertInstanceOf('phpDocumentor\\Descriptor\\Collection', $result);
     $this->assertSame(array($parentDescriptor, $grandParentDescriptor), $result->getAll());
 }
 /**
  * @covers phpDocumentor\Compiler\Pass\PackageTreeBuilder::execute
  * @covers phpDocumentor\Compiler\Pass\PackageTreeBuilder::addElementsOfTypeToPackage
  */
 public function testAddConstantToNamespace()
 {
     $constant = new ConstantDescriptor();
     $constant->setPackage('My\\Space');
     $constant->setFullyQualifiedStructuralElementName('My\\Space\\Constant1');
     $this->project->getFiles()->get(0)->getConstants()->add($constant);
     // double check if a second constant in the same deep namespace ends up at the right location
     $constant2 = new ConstantDescriptor();
     $constant2->setPackage('My\\Space');
     $constant2->setFullyQualifiedStructuralElementName('My\\Space\\Constant2');
     $this->project->getFiles()->get(0)->getConstants()->add($constant2);
     $this->fixture->execute($this->project);
     $this->assertSame(array($constant, $constant2), $this->project->getIndexes()->get('packages')->get('\\My\\Space')->getConstants()->getAll());
 }
 /**
  * @covers phpDocumentor\Descriptor\ConstantDescriptor::getValue
  * @covers phpDocumentor\Descriptor\ConstantDescriptor::setValue
  */
 public function testSetAndGetValue()
 {
     $this->assertSame(null, $this->fixture->getValue());
     $this->fixture->setValue('a');
     $this->assertSame('a', $this->fixture->getValue());
 }
 /**
  * @covers phpDocumentor\Compiler\Pass\ElementsIndexBuilder::execute
  * @covers phpDocumentor\Compiler\Pass\ElementsIndexBuilder::addElementsToIndexes
  * @covers phpDocumentor\Compiler\Pass\ElementsIndexBuilder::getIndexKey
  * @covers phpDocumentor\Compiler\Pass\ElementsIndexBuilder::getSubElements
  */
 public function testAddClassConstantsToIndex()
 {
     $file1 = $this->project->getFiles()->get(0);
     $classDescriptor1 = new ClassDescriptor();
     $classDescriptor1->setFullyQualifiedStructuralElementName('My\\Space\\Class1');
     $file1->getClasses()->add($classDescriptor1);
     $classConstantDescriptor1 = new ConstantDescriptor();
     $classConstantDescriptor1->setFullyQualifiedStructuralElementName('My\\Space\\Class1::CONSTANT');
     $classDescriptor1->getConstants()->add($classConstantDescriptor1);
     $file2 = $this->project->getFiles()->get(1);
     $classDescriptor2 = new ClassDescriptor();
     $classDescriptor2->setFullyQualifiedStructuralElementName('My\\Space\\Class2');
     $file2->getClasses()->add($classDescriptor2);
     $classConstantDescriptor2 = new ConstantDescriptor();
     $classConstantDescriptor2->setFullyQualifiedStructuralElementName('My\\Space\\Class2::CONSTANT');
     $classDescriptor2->getConstants()->add($classConstantDescriptor2);
     $this->fixture->execute($this->project);
     $elements = $this->project->getIndexes()->get('elements')->getAll();
     $this->assertCount(4, $elements);
     $this->assertSame(array('My\\Space\\Class1', 'My\\Space\\Class1::CONSTANT', 'My\\Space\\Class2', 'My\\Space\\Class2::CONSTANT'), array_keys($elements));
     $this->assertSame(array($classDescriptor1, $classConstantDescriptor1, $classDescriptor2, $classConstantDescriptor2), array_values($elements));
     // class constants are not indexed separately
     $elements = $this->project->getIndexes()->get('constants')->getAll();
     $this->assertCount(0, $elements);
 }