/**
  * Filter Descriptor with based on visibility.
  *
  * @param DescriptorAbstract $value
  *
  * @return DescriptorAbstract|null
  */
 public function filter($value)
 {
     if ($value instanceof VisibilityInterface && !$this->builder->isVisibilityAllowed($value->getVisibility())) {
         return null;
     }
     return $value;
 }
 /**
  * @param string   $notation
  * @param string   $returnType
  * @param string   $name
  * @param string[] $arguments
  * @param string   $description
  *
  * @dataProvider provideNotations
  * @covers phpDocumentor\Descriptor\Builder\Reflector\Tags\MethodAssembler::create
  * @covers phpDocumentor\Descriptor\Builder\Reflector\Tags\MethodAssembler::createArgumentDescriptorForMagicMethod
  */
 public function testCreateMethodDescriptorFromVariousNotations($notation, $returnType, $name, $arguments = array(), $description = '')
 {
     $this->builder->shouldReceive('buildDescriptor')->with(m::on(function (TypeCollection $value) use($returnType) {
         return $value[0] == $returnType;
     }))->andReturn(new Collection(array($returnType)));
     foreach ($arguments as $argument) {
         list($argumentType, $argumentName, $argumentDefault) = $argument;
         $this->builder->shouldReceive('buildDescriptor')->with(m::on(function (TypeCollection $value) use($argumentType) {
             return $value[0] == $argumentType;
         }))->andReturn(new Collection(array($argumentType)));
     }
     $tag = new MethodTag('method', $notation);
     $descriptor = $this->fixture->create($tag);
     $this->assertSame(1, $descriptor->getResponse()->getTypes()->count());
     $this->assertSame($returnType, $descriptor->getResponse()->getTypes()->get(0));
     $this->assertSame($name, $descriptor->getMethodName());
     $this->assertSame($description, $descriptor->getDescription());
     $this->assertSame(count($arguments), $descriptor->getArguments()->count());
     foreach ($arguments as $argument) {
         list($argumentType, $argumentName, $argumentDefault) = $argument;
         $this->assertSame($argumentType, $descriptor->getArguments()->get($argumentName)->getTypes()->get(0));
         $this->assertSame($argumentName, $descriptor->getArguments()->get($argumentName)->getName());
         $this->assertSame($argumentDefault, $descriptor->getArguments()->get($argumentName)->getDefault());
     }
 }
 /**
  * Creates a new fixture to test with.
  */
 protected function setUp()
 {
     $this->builderMock = m::mock('phpDocumentor\\Descriptor\\ProjectDescriptorBuilder');
     $this->builderMock->shouldReceive('buildDescriptor')->andReturn(null);
     $this->argumentAssemblerMock = m::mock('phpDocumentor\\Descriptor\\Builder\\Reflector\\ArgumentAssembler');
     $this->argumentAssemblerMock->shouldReceive('getBuilder')->andReturn($this->builderMock);
     $this->fixture = new MethodAssembler($this->argumentAssemblerMock);
     $this->fixture->setBuilder($this->builderMock);
 }
예제 #4
0
 /**
  * Creates a new FileReflector for the given filename or null if the file contains no modifications.
  *
  * @param ProjectDescriptorBuilder $builder
  * @param string                   $filename
  *
  * @return FileReflector|null Returns a new FileReflector or null if no modifications were detected for the given
  *     filename.
  */
 protected function createFileReflector(ProjectDescriptorBuilder $builder, $filename)
 {
     $file = new FileReflector($filename, $this->parser->doValidation(), $this->parser->getEncoding());
     $file->setDefaultPackageName($this->parser->getDefaultPackageName());
     $file->setMarkers($this->parser->getMarkers());
     $file->setFilename($this->getRelativeFilename($filename));
     $cachedFiles = $builder->getProjectDescriptor()->getFiles();
     $hash = $cachedFiles->get($file->getFilename()) ? $cachedFiles->get($file->getFilename())->getHash() : null;
     return $hash === $file->getHash() && !$this->parser->isForced() ? null : $file;
 }
 /**
  * @covers phpDocumentor\Descriptor\Builder\Reflector\Tags\ReturnAssembler::create
  */
 public function testCreatingReturnDescriptorFromReflector()
 {
     $types = new Collection();
     $this->builder->shouldReceive('buildDescriptor')->with(m::on(function ($value) {
         return $value instanceof TypeCollection && $value[0] == 'string';
     }))->andReturn($types);
     $reflector = new ReturnTag('return', 'string This is a description');
     $descriptor = $this->fixture->create($reflector);
     $this->assertSame('return', $descriptor->getName());
     $this->assertSame('This is a description', $descriptor->getDescription());
     $this->assertSame($types, $descriptor->getTypes());
 }
예제 #6
0
 /**
  * If the ProjectDescriptor's settings allow internal tags then return the Descriptor, otherwise null to filter it.
  *
  * @param DescriptorAbstract $value
  *
  * @return DescriptorAbstract|null
  */
 public function filter($value)
 {
     $isInternalAllowed = $this->builder->isVisibilityAllowed(Settings::VISIBILITY_INTERNAL);
     if ($isInternalAllowed) {
         $value->setDescription(preg_replace('/{@internal\\ (.+)}}/', '$1', $value->getDescription()));
         return $value;
     }
     // remove inline @internal tags
     $value->setDescription(preg_replace('/{@internal\\ .+}}/', '', $value->getDescription()));
     // if internal elements are not allowed; filter this element
     if ($value->getTags()->get('internal')) {
         return null;
     }
     return $value;
 }
 /**
  * @param $expected
  * @return Collection
  */
 protected function thenProjectBuilderShouldSetCollectionOfExpectedTypes($expected)
 {
     $types = new Collection($expected);
     $this->builderMock->shouldReceive('buildDescriptor')->with(m::on(function ($value) use($expected) {
         return $value instanceof Collection && $value->getArrayCopy() == $expected;
     }))->andReturn($types);
     return $types;
 }
예제 #8
0
 /**
  * Returns a relative URL from the webroot if the given FQSEN exists in the project.
  *
  * Example usage inside template would be (where @link is an attribute called link):
  *
  * ```
  * <xsl:value-of select="php:function('phpDocumentor\Plugin\Core\Xslt\Extension::path', string(@link))" />
  * ```
  *
  * @param string $fqsen
  *
  * @return bool|string
  */
 public static function path($fqsen)
 {
     $projectDescriptor = self::$descriptorBuilder->getProjectDescriptor();
     $elementList = $projectDescriptor->getIndexes()->get('elements');
     $node = $fqsen;
     if (isset($elementList[$fqsen])) {
         $node = $elementList[$fqsen];
     } elseif (isset($elementList['~\\' . $fqsen])) {
         $node = $elementList['~\\' . $fqsen];
     }
     $rule = self::$routers->match($node);
     if (!$rule) {
         return '';
     }
     $generatedUrl = $rule->generate($node);
     return $generatedUrl ? ltrim($generatedUrl, '/') : false;
 }
 /**
  * @covers phpDocumentor\Descriptor\ProjectDescriptorBuilder::isVisibilityAllowed
  */
 public function testDeterminesWhetherASpecificVisibilityIsAllowedToBeIncluded()
 {
     $projectDescriptorName = 'My Descriptor';
     $projectDescriptorMock = new ProjectDescriptor($projectDescriptorName);
     $projectDescriptorMock->getSettings()->setVisibility(Settings::VISIBILITY_PUBLIC);
     $this->fixture->setProjectDescriptor($projectDescriptorMock);
     $this->assertTrue($this->fixture->isVisibilityAllowed(Settings::VISIBILITY_PUBLIC));
     $this->assertFalse($this->fixture->isVisibilityAllowed(Settings::VISIBILITY_PRIVATE));
 }
 /**
  * @covers phpDocumentor\Descriptor\Filter\StripInternal::filter
  */
 public function testDescriptorIsUnmodifiedIfThereIsNoInternalTag()
 {
     $this->builderMock->shouldReceive('isVisibilityAllowed')->andReturn(true);
     $descriptor = m::mock('phpDocumentor\\Descriptor\\DescriptorAbstract');
     $descriptor->shouldReceive('getDescription');
     $descriptor->shouldReceive('setDescription');
     $descriptor->shouldReceive('getTags->get')->with('internal')->andReturn(false);
     // we clone the descriptor so its references differ; if something changes in the descriptor then
     // the $descriptor variable and the returned clone will differ
     $this->assertEquals($descriptor, $this->fixture->filter(clone $descriptor));
 }
예제 #11
0
 /**
  * Adds the Building mechanism using the key 'descriptor.builder'.
  *
  * Please note that the type of serializer can be configured using the parameter 'descriptor.builder.serializer'; it
  * accepts any parameter that Zend\Serializer supports.
  *
  * @param Application $app
  *
  * @return void
  */
 protected function addBuilder(Application $app)
 {
     if (extension_loaded('igbinary')) {
         $app['descriptor.builder.serializer'] = 'IgBinary';
     } else {
         $app['descriptor.builder.serializer'] = 'PhpSerialize';
     }
     $app['descriptor.builder'] = $app->share(function ($container) {
         $builder = new ProjectDescriptorBuilder($container['descriptor.builder.assembler.factory'], $container['descriptor.filter'], $container['validator']);
         $builder->setTranslator($container['translator']);
         return $builder;
     });
 }
 /**
  * @covers phpDocumentor\Descriptor\Filter\StripOnVisibility::filter
  */
 public function testKeepsDescriptorIfDescriptorNotInstanceOfVisibilityInterface()
 {
     $this->builderMock->shouldReceive('isVisibilityAllowed')->andReturn(false);
     $descriptor = m::mock('\\phpDocumentor\\Descriptor\\DescriptorAbstract');
     $this->assertSame($descriptor, $this->fixture->filter($descriptor));
 }
예제 #13
0
 /**
  * Checks if the settings of the project have changed and forces a complete rebuild if they have.
  *
  * @param ProjectDescriptorBuilder $builder
  *
  * @return void
  */
 protected function forceRebuildIfSettingsHaveModified(ProjectDescriptorBuilder $builder)
 {
     if ($builder->getProjectDescriptor()->getSettings()->isModified()) {
         $this->setForced(true);
         $this->log('One of the project\'s settings have changed, forcing a complete rebuild');
     }
 }
예제 #14
0
 /**
  * Iterates through the given files feeds them to the builder.
  *
  * @param ProjectDescriptorBuilder $builder
  * @param Collection               $files          A files container to parse.
  *
  * @api
  *
  * @throws Exception if no files were found.
  *
  * @return bool|string
  */
 public function parse(ProjectDescriptorBuilder $builder, Collection $files)
 {
     $timer = microtime(true);
     $paths = $this->getFilenames($files);
     $this->log('  Project root is:  ' . $files->getProjectRoot());
     $this->log('  Ignore paths are: ' . implode(', ', $files->getIgnorePatterns()->getArrayCopy()));
     if ($builder->getProjectDescriptor()->getSettings()->isModified()) {
         $this->setForced(true);
         $this->log('One of the project\'s settings have changed, forcing a complete rebuild');
     }
     foreach ($paths as $filename) {
         if (class_exists('phpDocumentor\\Event\\Dispatcher')) {
             Dispatcher::getInstance()->dispatch('parser.file.pre', PreFileEvent::createInstance($this)->setFile($filename));
         }
         $this->log('Starting to parse file: ' . $filename);
         $memory = memory_get_usage();
         try {
             $file = new FileReflector($filename, $this->doValidation(), $this->getEncoding());
             $file->setDefaultPackageName($this->getDefaultPackageName());
             $file->setMarkers($this->getMarkers());
             $file->setFilename($this->getRelativeFilename($filename));
             // if the hash is unchanged; continue to the next file
             $cachedFiles = $builder->getProjectDescriptor()->getFiles();
             $hash = $cachedFiles->get($file->getFilename()) ? $cachedFiles->get($file->getFilename())->getHash() : null;
             if ($hash === $file->getHash() && !$this->isForced()) {
                 $this->log('>> Skipped file ' . $file->getFilename() . ' as no modifications were detected');
                 continue;
             }
             $file->process();
             $builder->buildFileUsingSourceData($file);
             $fileDescriptor = $builder->getProjectDescriptor()->getFiles()->get($file->getFilename());
             $errors = $fileDescriptor->getAllErrors();
             foreach ($errors as $error) {
                 $this->log($error->getCode(), $error->getSeverity(), $error->getContext());
             }
         } catch (Exception $e) {
             $this->log('  Unable to parse file "' . $filename . '", an error was detected: ' . $e->getMessage(), LogLevel::ALERT);
         }
         $memoryDelta = memory_get_usage() - $memory;
         $this->log('>> Memory after processing of file: ' . number_format(memory_get_usage() / 1024 / 1024, 2) . ' megabytes (' . ($memoryDelta > -0 ? '+' : '') . number_format($memoryDelta / 1024) . ' kilobytes)', LogLevel::DEBUG);
     }
     $this->log('Elapsed time to parse all files: ' . round(microtime(true) - $timer, 2) . 's');
     $this->log('Peak memory usage: ' . round(memory_get_peak_usage() / 1024 / 1024, 2) . 'M');
     return $builder->getProjectDescriptor();
 }