Skip to content

Commit

Permalink
fix PhpDoc who was broken by the bundle update
Browse files Browse the repository at this point in the history
  • Loading branch information
Webonaute committed May 8, 2017
1 parent 2c5a1dc commit 2abd1a1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 55 deletions.
101 changes: 53 additions & 48 deletions Extraction/Extractor/PhpDocOperationExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,17 @@
use Draw\Swagger\Extraction\ExtractorInterface;
use Draw\Swagger\Schema\BodyParameter;
use Draw\Swagger\Schema\Operation;
use Draw\Swagger\Schema\QueryParameter;
use Draw\Swagger\Schema\Response;
use Draw\Swagger\Schema\Schema;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\Types\Object_;
use ReflectionMethod;

class PhpDocOperationExtractor implements ExtractorInterface
{
private $exceptionResponseCodes = array();

/**
* Return if the extractor can extract the requested data or not.
*
* @param $source
* @param $type
* @param ExtractionContextInterface $extractionContext
* @return boolean
*/
public function canExtract($source, $type, ExtractionContextInterface $extractionContext)
{
if (!$source instanceof ReflectionMethod) {
return false;
}

if (!$type instanceof Operation) {
return false;
}

return true;
}
private $exceptionResponseCodes = [];

/**
* Extract the requested data.
Expand All @@ -53,41 +35,42 @@ public function extract($method, $operation, ExtractionContextInterface $extract
throw new ExtractionImpossibleException();
}

$docBlock = new DocBlock($method->getDocComment());
$factory = DocBlockFactory::createInstance();
$docBlock = $factory->create($method->getDocComment());

if(!$operation->summary) {
if (!$operation->summary) {
$operation->summary = $docBlock->getSummary();
}

if($operation->description) {
if ($operation->description) {
$operation->description = $docBlock->getDescription();
}

/** @var DocBlock\Tags\Return_ $returnTag */
foreach ($docBlock->getTagsByName('return') as $returnTag) {
/* @var $returnTag \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag */
foreach ($returnTag->getTypes() as $type) {
$response = new Response();
$response->schema = $responseSchema = new Schema();
$response->description = $returnTag->getDescription();
$operation->responses[200] = $response;
/** @var Object_ $type */
$type = $returnTag->getType();
$response = new Response();
$response->schema = $responseSchema = new Schema();
$response->description = $returnTag->getDescription();
$operation->responses[200] = $response;

$subContext = $extractionContext->createSubContext();
$subContext->setParameter('direction','out');
$subContext = $extractionContext->createSubContext();
$subContext->setParameter('direction', 'out');

$extractionContext->getSwagger()->extract((string)$type, $responseSchema, $subContext);

$extractionContext->getSwagger()->extract($type, $responseSchema, $subContext);
}
}

if($docBlock->getTagsByName('deprecated')) {
$operation->deprecated = true;
if ($docBlock->getTagsByName('deprecated')) {
$operation->deprecated = true;
}

/* @var $throwTag \phpDocumentor\Reflection\DocBlock\Tags\Throws */
foreach ($docBlock->getTagsByName('throws') as $throwTag) {
/* @var $throwTag \phpDocumentor\Reflection\DocBlock\Tags\Throws */


/** @var Object_ $type */
$type = $throwTag->getType();
$exceptionClass = new \ReflectionClass($type);
$exceptionClass = new \ReflectionClass((string)$type);
$exception = $exceptionClass->newInstanceWithoutConstructor();
list($code, $message) = $this->getExceptionInformation($exception);
$operation->responses[$code] = $exceptionResponse = new Response();
Expand All @@ -113,11 +96,11 @@ public function extract($method, $operation, ExtractionContextInterface $extract
}
}

/** @var \phpDocumentor\Reflection\DocBlock\Tags\Param $paramTag */
foreach ($docBlock->getTagsByName('param') as $paramTag) {
/* @var $paramTag \phpDocumentor\Reflection\DocBlock\Tag\ParamTag */

$parameterName = trim($paramTag->getVariableName(), '$');

/** @var QueryParameter $parameter */
$parameter = null;
foreach ($operation->parameters as $existingParameter) {
if ($existingParameter->name == $parameterName) {
Expand All @@ -132,7 +115,7 @@ public function extract($method, $operation, ExtractionContextInterface $extract
}

if (!$parameter->type) {
$parameter->type = $paramTag->getType();
$parameter->type = (string) $paramTag->getType();
}
continue;
}
Expand All @@ -148,8 +131,8 @@ public function extract($method, $operation, ExtractionContextInterface $extract

if (!$parameter->type) {
$subContext = $extractionContext->createSubContext();
$subContext->setParameter('direction','in');
$extractionContext->getSwagger()->extract($paramTag->getType(), $parameter, $subContext);
$subContext->setParameter('direction', 'in');
$extractionContext->getSwagger()->extract( (string) $paramTag->getType(), $parameter, $subContext);
}

continue;
Expand All @@ -158,6 +141,28 @@ public function extract($method, $operation, ExtractionContextInterface $extract
}
}

/**
* Return if the extractor can extract the requested data or not.
*
* @param $source
* @param $type
* @param ExtractionContextInterface $extractionContext
*
* @return boolean
*/
public function canExtract($source, $type, ExtractionContextInterface $extractionContext)
{
if (!$source instanceof ReflectionMethod) {
return false;
}

if (!$type instanceof Operation) {
return false;
}

return true;
}

private function getExceptionInformation(\Exception $exception)
{
foreach ($this->exceptionResponseCodes as $class => $information) {
Expand All @@ -166,11 +171,11 @@ private function getExceptionInformation(\Exception $exception)
}
}

return array(500, null);
return [500, null];
}

public function registerExceptionResponseCodes($exceptionClass, $code = 500, $message = null)
{
$this->exceptionResponseCodes[$exceptionClass] = array($code, $message);
$this->exceptionResponseCodes[$exceptionClass] = [$code, $message];
}
}
4 changes: 4 additions & 0 deletions Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,8 @@ public function preSerialize()
$this->example = Mixed::convert($this->example);
$this->enum = Mixed::convert($this->enum, true);
}

/*
*
*/
}
18 changes: 11 additions & 7 deletions Swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public function registerExtractor(ExtractorInterface $extractorInterface, $posit
$this->extractors[$section][$position][] = $extractorInterface;
}

/**
* @param Schema $schema
* @return string
*/
public function dump(Schema $schema)
{
public function validate(Schema $schema){
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(new AnnotationReader())
->getValidator();
Expand All @@ -70,7 +65,15 @@ public function dump(Schema $schema)
if(count($result)) {
throw new \InvalidArgumentException("" . $result);
}
}

/**
* @param Schema $schema
* @return string
*/
public function dump(Schema $schema)
{
$this->validate($schema);
return $this->serializer->serialize($schema, 'json');
}

Expand All @@ -89,7 +92,8 @@ public function extract($source, $type = null, ExtractionContextInterface $extra
$extractionContext = new ExtractionContext($this, $type);
}

foreach ($this->getSortedExtractors() as $extractor) {
$sortedExtractors = $this->getSortedExtractors();
foreach ($sortedExtractors as $extractor) {
if ($extractor->canExtract($source, $type, $extractionContext)) {
$extractor->extract($source, $type, $extractionContext);
}
Expand Down
1 change: 1 addition & 0 deletions tests/fixture/input/base/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"version": "1.0"
},
"tags": {},
"paths": {
"/service": {}
}
Expand Down
1 change: 1 addition & 0 deletions tests/fixture/schema/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
},
"version": "1.0"
},
"tags": {},
"x-test": "test"
}

0 comments on commit 2abd1a1

Please sign in to comment.