/** * @covers phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Classes\HasSinglePackageValidator::validate */ public function testValidateSinglePackage() { $packageCollection = new Collection(array('x')); $tagCollection = new Collection(array('package' => $packageCollection)); $this->fileDescriptor->shouldReceive('getTags')->andReturn($tagCollection)->once(); $this->context->shouldReceive('addViolationAt')->never(); $this->validator->validate($this->fileDescriptor, $this->constraint); }
/** * 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); } }
/** * @codingStandardsIgnoreStart * @covers phpDocumentor\Plugin\Core\Descriptor\Validator\Constraints\Classes\HasPackageWithSubpackageValidator::validate * @codingStandardsIgnoreEnd */ public function testValidateSubpackageWithPackage() { $packageCollection = new Collection(array('x')); $subpackageCollection = new Collection(array('y')); $tagPackageCollection = new Collection(array('package' => $packageCollection, 'subpackage' => $subpackageCollection)); $this->fileDescriptor->shouldReceive('getTags')->andReturn($tagPackageCollection)->twice(); $this->context->shouldReceive('addViolationAt')->never(); $this->validator->validate($this->fileDescriptor, $this->constraint); $this->assertTrue(true); }
/** * Registers the markers that were found in a File with the File Descriptor. * * @param string[] $markers * @param FileDescriptor $fileDescriptor * * @return void */ protected function addMarkers($markers, $fileDescriptor) { foreach ($markers as $marker) { list($type, $message, $line) = $marker; $fileDescriptor->getMarkers()->add(array('type' => $type, 'message' => $message, 'line' => $line)); } }
/** * Adds a marker with the TO DO information to the file on a given line number. * * @param FileDescriptor $fileDescriptor * @param TagDescriptor $todo * @param integer $lineNumber * * @return void */ protected function addTodoMarkerToFile($fileDescriptor, $todo, $lineNumber) { $fileDescriptor->getMarkers()->add(array('type' => 'TODO', 'message' => $todo->getDescription(), 'line' => $lineNumber)); }
protected function buildFile(\DOMElement $parent, FileDescriptor $file, Transformer $transformer) { $child = new \DOMElement('file'); $parent->appendChild($child); $path = ltrim($file->getPath(), './'); $child->setAttribute('path', $path); $child->setAttribute('generated-path', $transformer->generateFilename($path)); $child->setAttribute('hash', $file->getHash()); $this->buildDocBlock($child, $file); // add namespace aliases foreach ($file->getNamespaceAliases() as $alias => $namespace) { $alias_obj = new \DOMElement('namespace-alias', $namespace); $child->appendChild($alias_obj); $alias_obj->setAttribute('name', $alias); } /** @var ConstantDescriptor $constant */ foreach ($file->getConstants() as $constant) { $this->buildConstant($child, $constant); } /** @var FunctionDescriptor $function */ foreach ($file->getFunctions() as $function) { $this->buildFunction($child, $function); } /** @var InterfaceDescriptor $interface */ foreach ($file->getInterfaces() as $interface) { $this->buildInterface($child, $interface); } /** @var ClassDescriptor $class */ foreach ($file->getClasses() as $class) { $this->buildClass($child, $class); } // add markers if (count($file->getMarkers()) > 0) { $markers = new \DOMElement('markers'); $child->appendChild($markers); foreach ($file->getMarkers() as $marker) { $marker_obj = new \DOMElement(strtolower($marker['type'])); $markers->appendChild($marker_obj); $marker_obj->appendChild(new \DOMText(trim($marker['message']))); $marker_obj->setAttribute('line', $marker['line']); } } if (count($file->getErrors()) > 0) { $parse_errors = new \DOMElement('parse_markers'); $child->appendChild($parse_errors); /** @var Error $error */ foreach ($file->getAllErrors() as $error) { $this->createErrorEntry($error, $parse_errors); } } // if we want to include the source for each file; append a new // element 'source' which contains a compressed, encoded version // of the source if ($file->getSource()) { $child->appendChild(new \DOMElement('source', base64_encode(gzcompress($file->getSource())))); } }
/** * Writes the errors found in the Descriptor to the log. * * @param FileDescriptor $fileDescriptor * * @return void */ protected function logErrorsForDescriptor(FileDescriptor $fileDescriptor) { $errors = $fileDescriptor->getAllErrors(); /** @var Error $error */ foreach ($errors as $error) { $this->log($error->getCode(), $error->getSeverity(), $error->getContext()); } }
/** * Returns the path to the file containing this element relative to the project's root. * * @return string */ public function getPath() { return $this->fileDescriptor ? $this->fileDescriptor->getPath() : ''; }
/** * @covers phpDocumentor\Descriptor\FileDescriptor::__construct * @covers phpDocumentor\Descriptor\FileDescriptor::getAllErrors */ public function testGetAllErrors() { /* * constant * function * class * property * constant * method * interface * constant * method * traits * property * method */ // setup error list $errorGlobal = array('error-global'); $errorClasses = array('error-class'); $errorClassMethods = array('error-class-method'); $errorClassConstants = array('error-class-constant'); $errorClassProperties = array('error-class-property'); $errorInterfaces = array('error-interface'); $errorInterfacesConstants = array('error-interface-constant'); $errorInterfacesMethods = array('error-interface-method'); $errorTraits = array('error-traits'); $errorTraitsProperties = array('error-traits-property'); $errorTraitsMethods = array('error-traits-method'); $errorFunctions = array('error-functions'); // setup global check $collection = new Collection($errorGlobal); $this->fixture->setErrors($collection); // setup class-property check $mockClassProperties = m::mock('phpDocumentor\\Descriptor\\PropertyDescriptor'); $mockClassProperties->shouldReceive('getErrors')->andReturn(new Collection($errorClassProperties)); // setup class-constant check $mockClassConstants = m::mock('phpDocumentor\\Descriptor\\ConstantDescriptor'); $mockClassConstants->shouldReceive('getErrors')->andReturn(new Collection($errorClassConstants)); // setup class-method check $mockClassMethods = m::mock('phpDocumentor\\Descriptor\\MethodDescriptor'); $mockClassMethods->shouldReceive('getErrors')->andReturn(new Collection($errorClassMethods)); // setup class check $mockClasses = m::mock('phpDocumentor\\Descriptor\\ClassDescriptor'); $mockClasses->shouldReceive('getProperties')->andReturn(new Collection(array($mockClassProperties))); $mockClasses->shouldReceive('getConstants')->andReturn(new Collection(array($mockClassConstants))); $mockClasses->shouldReceive('getMethods')->andReturn(new Collection(array($mockClassMethods))); $mockClasses->shouldReceive('getErrors')->andReturn(new Collection($errorClasses)); $this->fixture->getClasses()->set('my-test-class', $mockClasses); // setup interface-constant check $mockInterfaceConstants = m::mock('phpDocumentor\\Descriptor\\ConstantDescriptor'); $mockInterfaceConstants->shouldReceive('getErrors')->andReturn(new Collection($errorInterfacesConstants)); // setup interface-method check $mockInterfaceMethods = m::mock('phpDocumentor\\Descriptor\\MethodDescriptor'); $mockInterfaceMethods->shouldReceive('getErrors')->andReturn(new Collection($errorInterfacesMethods)); // setup interface check $mockInterfaces = m::mock('phpDocumentor\\Descriptor\\ClassDescriptor'); $mockInterfaces->shouldReceive('getProperties')->andReturn(array()); $mockInterfaces->shouldReceive('getConstants')->andReturn(new Collection(array($mockInterfaceConstants))); $mockInterfaces->shouldReceive('getMethods')->andReturn(new Collection(array($mockInterfaceMethods))); $mockInterfaces->shouldReceive('getErrors')->andReturn(new Collection($errorInterfaces)); $this->fixture->getClasses()->set('my-test-interface', $mockInterfaces); // setup traits-constant check $mockTraitsProperties = m::mock('phpDocumentor\\Descriptor\\ConstantDescriptor'); $mockTraitsProperties->shouldReceive('getErrors')->andReturn(new Collection($errorTraitsProperties)); // setup traits-method check $mockTraitsMethods = m::mock('phpDocumentor\\Descriptor\\MethodDescriptor'); $mockTraitsMethods->shouldReceive('getErrors')->andReturn(new Collection($errorTraitsMethods)); // setup traits check $mockTraits = m::mock('phpDocumentor\\Descriptor\\ClassDescriptor'); $mockTraits->shouldReceive('getConstants')->andReturn(array()); $mockTraits->shouldReceive('getProperties')->andReturn(new Collection(array($mockTraitsProperties))); $mockTraits->shouldReceive('getMethods')->andReturn(new Collection(array($mockTraitsMethods))); $mockTraits->shouldReceive('getErrors')->andReturn(new Collection($errorTraits)); $this->fixture->getClasses()->set('my-test-traits', $mockTraits); // setup functions check $mockFunctions = m::mock('phpDocumentor\\Descriptor\\FunctionDescriptor'); // create dummy instances of constants/methods $mockFunctions->shouldReceive('getConstants')->andReturn(array()); $mockFunctions->shouldReceive('getProperties')->andReturn(array()); $mockFunctions->shouldReceive('getMethods')->andReturn(array()); $mockFunctions->shouldReceive('getErrors')->andReturn(new Collection($errorFunctions)); $this->fixture->getClasses()->set('my-test-function', $mockFunctions); // final merge and check $expectedErrors = array_merge($errorGlobal, $errorClasses, $errorInterfaces, $errorTraits, $errorFunctions, $errorClassMethods, $errorClassConstants, $errorClassProperties, $errorInterfacesMethods, $errorInterfacesConstants, $errorTraitsMethods, $errorTraitsProperties); $this->assertSame($expectedErrors, $this->fixture->getAllErrors()->getAll()); }
/** * Generates a URL from the given node or returns false if unable. * * @param string|Descriptor\FileDescriptor $node * * @return string|false */ public function __invoke($node) { $converter = new QualifiedNameToUrlConverter(); return '/files/' . $converter->fromFile($node->getPath()) . '.html'; }