/** * Registers a parent class or interface with this constant. * * @param ClassDescriptor|InterfaceDescriptor|null $parent * * @throws \InvalidArgumentException if anything other than a class, interface or null was passed. * * @return void */ public function setParent($parent) { if (!$parent instanceof ClassDescriptor && !$parent instanceof InterfaceDescriptor && $parent !== null) { throw new \InvalidArgumentException('Constants can only have an interface or class as parent'); } $this->setFullyQualifiedStructuralElementName($parent->getFullyQualifiedStructuralElementName() . '::' . $this->getName()); $this->parent = $parent; }
/** * Checks if the passed value is valid. * * @param FileDescriptor|ClassDescriptor|InterfaceDescriptor|TraitDescriptor $value The value that should * be validated. * @param Constraint $constraint The constraint for * the validation. * * @throws ConstraintDefinitionException if this is not a constraint on a PropertyDescriptor object. */ public function validate($value, Constraint $constraint) { if (!$value instanceof FileDescriptor && !$value instanceof ClassDescriptor && !$value instanceof InterfaceDescriptor && !$value instanceof TraitDescriptor) { throw new ConstraintDefinitionException('The HasPackageWithSubpackageValidator validator may only be used on files, classes, ' . 'interfaces and traits'); } if ($value->getTags()->get('subpackage', new Collection())->count() > 0 && $value->getTags()->get('package', new Collection())->count() < 1) { $this->context->addViolationAt('package', $constraint->message, array(), null, null, $constraint->code); } }
/** * Checks if the passed value is valid. * * @param FileDescriptor|ClassDescriptor|InterfaceDescriptor|TraitDescriptor $value The value that should * be validated. * @param Constraint $constraint The constraint for * the validation. * * @throws ConstraintDefinitionException if this is not a constraint on a PropertyDescriptor object. */ public function validate($value, Constraint $constraint) { if (!$value instanceof FileDescriptor && !$value instanceof ClassDescriptor && !$value instanceof InterfaceDescriptor && !$value instanceof TraitDescriptor) { throw new ConstraintDefinitionException('The HasSinglePackage validator may only be used on files, classes, interfaces and traits'); } if ($value->getTags()->get('package', new Collection())->count() > 1) { $this->context->addViolationAt('package', $constraint->message); } }
/** * Creates a Descriptor from the provided data. * * @param InterfaceReflector $data * * @return InterfaceDescriptor */ public function create($data) { $interfaceDescriptor = new InterfaceDescriptor(); $interfaceDescriptor->setFullyQualifiedStructuralElementName($data->getName()); $interfaceDescriptor->setName($data->getShortName()); $interfaceDescriptor->setLine($data->getLinenumber()); $interfaceDescriptor->setPackage($this->extractPackageFromDocBlock($data->getDocBlock()) ?: ''); // Reflection library formulates namespace as global but this is not wanted for phpDocumentor itself $interfaceDescriptor->setNamespace('\\' . (strtolower($data->getNamespace()) == 'global' ? '' : $data->getNamespace())); $this->assembleDocBlock($data->getDocBlock(), $interfaceDescriptor); $this->addConstants($data->getConstants(), $interfaceDescriptor); $this->addMethods($data->getMethods(), $interfaceDescriptor); foreach ($data->getParentInterfaces() as $interfaceClassName) { $interfaceDescriptor->getParent()->set($interfaceClassName, $interfaceClassName); } return $interfaceDescriptor; }
/** * @covers phpDocumentor\Descriptor\InterfaceDescriptor::getInheritedMethods */ public function testRetrievingInheritedMethodsReturnsCollectionWithParent() { $parentDescriptor = new MethodDescriptor(); $parentDescriptor->setName('parent'); $parentDescriptorCollection = new Collection(); $parentDescriptorCollection->add($parentDescriptor); $parent = new InterfaceDescriptor(); $parent->setMethods($parentDescriptorCollection); $parentCollection = new Collection(); $parentCollection->add($parent); $grandParentDescriptor = new MethodDescriptor(); $grandParentDescriptor->setName('grandparent'); $grandParentDescriptorCollection = new Collection(); $grandParentDescriptorCollection->add($grandParentDescriptor); $grandParent = new InterfaceDescriptor(); $grandParent->setMethods($grandParentDescriptorCollection); $grandParentCollection = new Collection(); $grandParentCollection->add($grandParent); $parent->setParent($grandParentCollection); $this->fixture->setParent($parentCollection); $result = $this->fixture->getInheritedMethods(); $this->assertInstanceOf('phpDocumentor\\Descriptor\\Collection', $result); $this->assertSame(array($parentDescriptor, $grandParentDescriptor), $result->getAll()); }
/** * Export the given reflected interface definition to the provided parent 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 Element to augment. * @param InterfaceDescriptor $interface Element to export. * * @return \DOMElement */ public function convert(\DOMElement $parent, InterfaceDescriptor $interface) { $child = new \DOMElement('interface'); $parent->appendChild($child); /** @var InterfaceDescriptor $parentInterface */ foreach ($interface->getParent() as $parentInterface) { $parentFqcn = is_string($parentInterface) === false ? $parentInterface->getFullyQualifiedStructuralElementName() : $parentInterface; $child->appendChild(new \DOMElement('extends', $parentFqcn)); } $namespace = $interface->getNamespace()->getFullyQualifiedStructuralElementName(); $child->setAttribute('namespace', ltrim($namespace, '\\')); $child->setAttribute('line', $interface->getLine()); $child->appendChild(new \DOMElement('name', $interface->getName())); $child->appendChild(new \DOMElement('full_name', $interface->getFullyQualifiedStructuralElementName())); $this->docBlockConverter->convert($child, $interface); foreach ($interface->getConstants() as $constant) { $this->constantConverter->convert($child, $constant); } foreach ($interface->getMethods() as $method) { $this->methodConverter->convert($child, $method); } return $child; }
/** * @param $interfaceParent * @return InterfaceDescriptor */ protected function givenAnInterfaceWithParent($interfaceParent) { $classDescriptor3 = new InterfaceDescriptor(); $classDescriptor3->setParent($interfaceParent); return $classDescriptor3; }
/** * @param string $name The name of the current method. * * @return MethodDescriptor */ protected function whenFixtureHasMethodInImplementedInterfaceWithSameName($name) { $result = new MethodDescriptor(); $result->setName($name); $parent = new InterfaceDescriptor(); $parent->getMethods()->set($name, $result); $class = new ClassDescriptor(); $class->getInterfaces()->set('Implemented', $parent); $this->fixture->setParent($class); return $result; }
/** * @covers phpDocumentor\Compiler\Pass\PackageTreeBuilder::execute * @covers phpDocumentor\Compiler\Pass\PackageTreeBuilder::addElementsOfTypeToPackage */ public function testAddInterfaceToNamespace() { $interface = new InterfaceDescriptor(); $interface->setPackage('My\\Space'); $interface->setFullyQualifiedStructuralElementName('My\\Space\\Interface1'); $this->project->getFiles()->get(0)->getInterfaces()->add($interface); // double check if a second interface in the same deep namespace ends up at the right location $interface2 = new InterfaceDescriptor(); $interface2->setPackage('My\\Space'); $interface2->setFullyQualifiedStructuralElementName('My\\Space\\Interface2'); $this->project->getFiles()->get(0)->getInterfaces()->add($interface2); $this->fixture->execute($this->project); $this->assertSame(array($interface, $interface2), $this->project->getIndexes()->get('packages')->get('\\My\\Space')->getInterfaces()->getAll()); }
/** * @covers phpDocumentor\Compiler\Pass\ElementsIndexBuilder::execute * @covers phpDocumentor\Compiler\Pass\ElementsIndexBuilder::addElementsToIndexes * @covers phpDocumentor\Compiler\Pass\ElementsIndexBuilder::getIndexKey */ public function testAddInterfacesToIndex() { $file1 = $this->project->getFiles()->get(0); $interfaceDescriptor1 = new InterfaceDescriptor(); $interfaceDescriptor1->setFullyQualifiedStructuralElementName('My\\Space\\Interface1'); $file1->getInterfaces()->add($interfaceDescriptor1); $file2 = $this->project->getFiles()->get(1); $interfaceDescriptor2 = new InterfaceDescriptor(); $interfaceDescriptor2->setFullyQualifiedStructuralElementName('My\\Space\\Interface2'); $file2->getInterfaces()->add($interfaceDescriptor2); $this->fixture->execute($this->project); $elements = $this->project->getIndexes()->get('elements')->getAll(); $this->assertCount(2, $elements); $this->assertSame(array('My\\Space\\Interface1', 'My\\Space\\Interface2'), array_keys($elements)); $this->assertSame(array($interfaceDescriptor1, $interfaceDescriptor2), array_values($elements)); $elements = $this->project->getIndexes()->get('interfaces')->getAll(); $this->assertCount(2, $elements); $this->assertSame(array('My\\Space\\Interface1', 'My\\Space\\Interface2'), array_keys($elements)); $this->assertSame(array($interfaceDescriptor1, $interfaceDescriptor2), array_values($elements)); }
/** * @return InterfaceDescriptor */ protected function whenFixtureHasParentInterface() { $interface = new InterfaceDescriptor(); $this->fixture->getParent()->set('IA', $interface); return $interface; }