From b723f40f2041213c339c511978e3591914da88a0 Mon Sep 17 00:00:00 2001 From: Martin Poirier-Theoret Date: Tue, 24 Mar 2020 18:35:06 -0400 Subject: [PATCH] clean code and fixe some dependencies --- .travis.yml | 8 +-- Builder.php | 11 ++-- Extraction/ExtractionContext.php | 19 +++--- Extraction/ExtractionContextInterface.php | 62 +++++-------------- .../Constraint/ChoiceConstraintExtractor.php | 13 ++-- .../Constraint/CountConstraintExtractor.php | 2 +- .../Constraint/LengthConstraintExtractor.php | 2 +- .../NotBlankConstraintExtractor.php | 2 +- .../Constraint/NotNullConstraintExtractor.php | 2 +- .../Constraint/RangeConstraintExtractor.php | 2 +- Extraction/Extractor/ConstraintExtractor.php | 8 ++- .../DoctrineInheritanceExtractor.php | 12 ++-- .../DynamicObjectTypeToSchemaHandler.php | 1 - .../Extractor/PhpDocOperationExtractor.php | 48 +++++++------- .../Extractor/SwaggerParameterExtractor.php | 5 +- Extraction/Extractor/TypeSchemaExtractor.php | 5 +- JMSSerializerListener.php | 7 ++- Swagger.php | 25 ++++---- .../Extraction/Extractor/JmsExtractorTest.php | 11 ++-- .../PhpDocOperationExtractorTest.php | 25 +++++--- .../Extractor/SwaggerSchemaExtractorTest.php | 3 +- Tests/bootstrap.php | 2 +- Tests/fixture/schema/swagger.json | 1 + composer.json | 11 ++-- 24 files changed, 132 insertions(+), 155 deletions(-) diff --git a/.travis.yml b/.travis.yml index aca7829..e5e0804 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,8 @@ language: php php: - "7.2" - -env: - - JMS_SERIALIZER_VERSION="~1.0" - - JMS_SERIALIZER_VERSION="~2.0" - - JMS_SERIALIZER_VERSION="~3.0" + - "7.3" + - "7.4" before_script: - - composer require jms/serializer:${JMS_SERIALIZER_VERSION} - composer install --dev diff --git a/Builder.php b/Builder.php index 806ab98..3b84e26 100644 --- a/Builder.php +++ b/Builder.php @@ -2,9 +2,7 @@ use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\Reader; -use Doctrine\Common\Persistence\ManagerRegistry; -use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory; -use Doctrine\ORM\EntityManager; +use Doctrine\Persistence\ManagerRegistry; use Draw\Swagger\Extraction\Extractor\ConstraintExtractor; use Draw\Swagger\Extraction\Extractor\DoctrineInheritanceExtractor; use Draw\Swagger\Extraction\Extractor\JmsExtractor; @@ -15,9 +13,11 @@ use JMS\Serializer\EventDispatcher\EventDispatcher; use JMS\Serializer\Naming\CamelCaseNamingStrategy; use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; +use JMS\Serializer\SerializationContext; use JMS\Serializer\Serializer; use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerInterface; +use ReflectionClass; use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory; use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; @@ -202,7 +202,8 @@ public function build() if ($this->registerDefaultsExtractor) { if ($serializer instanceof Serializer) { - $metaDataFactory = $serializer->getMetadataFactory(); + $serializer->serialize([], 'json', $context = new SerializationContext()); + $metaDataFactory = $context->getMetadataFactory(); $swagger->registerExtractor( new JmsExtractor( $metaDataFactory, @@ -215,7 +216,7 @@ public function build() $extractName = pathinfo($file, PATHINFO_FILENAME); $className = 'Draw\Swagger\Extraction\Extractor\Constraint\\' . $extractName; - $reflection = new \ReflectionClass($className); + $reflection = new ReflectionClass($className); if (!$reflection->isInstantiable()) { continue; } diff --git a/Extraction/ExtractionContext.php b/Extraction/ExtractionContext.php index 4676d79..687dffc 100644 --- a/Extraction/ExtractionContext.php +++ b/Extraction/ExtractionContext.php @@ -25,20 +25,17 @@ public function __construct(Swagger $swagger, Schema $rootSchema) $this->swagger = $swagger; } - public function getRootSchema() + public function getRootSchema(): Schema { return $this->rootSchema; } - public function getSwagger() + public function getSwagger(): Swagger { return $this->swagger; } - /** - * @return mixed - */ - public function hasParameter($name) + public function hasParameter($name): bool { return array_key_exists($name, $this->parameters); } @@ -48,27 +45,27 @@ public function getParameter($name, $default = null) return $this->hasParameter($name) ? $this->parameters[$name] : $default; } - public function getParameters() + public function getParameters(): array { return $this->parameters; } - public function setParameter($name, $value) + public function setParameter($name, $value): void { $this->parameters[$name] = $value; } - public function removeParameter($name) + public function removeParameter($name): void { unset($this->parameters[$name]); } - public function setParameters(array $parameters) + public function setParameters(array $parameters): void { $this->parameters = $parameters; } - public function createSubContext() + public function createSubContext(): ExtractionContextInterface { return clone $this; } diff --git a/Extraction/ExtractionContextInterface.php b/Extraction/ExtractionContextInterface.php index 124f0a8..db8c9a1 100644 --- a/Extraction/ExtractionContextInterface.php +++ b/Extraction/ExtractionContextInterface.php @@ -2,53 +2,25 @@ namespace Draw\Swagger\Extraction; +use Draw\Swagger\Swagger; + interface ExtractionContextInterface { - /** - * @return \Draw\Swagger\Swagger - */ - public function getSwagger(); - - /** - * @return \Draw\Swagger\Schema\Swagger - */ - public function getRootSchema(); - - /** - * @return boolean - */ - public function hasParameter($name); - - /** - * @param $name - * @param null $default - * @return mixed - */ + public function getSwagger(): Swagger; + + public function getRootSchema(): \Draw\Swagger\Schema\Swagger; + + public function hasParameter($name): bool; + public function getParameter($name, $default = null); - /** - * @return array - */ - public function getParameters(); - - /** - * @param $name - * @param $value - */ - public function setParameter($name, $value); - - /** - * @param $name - */ - public function removeParameter($name); - - /** - * @param array $parameters - */ - public function setParameters(array $parameters); - - /** - * @return ExtractionContextInterface - */ - public function createSubContext(); + public function getParameters(): array; + + public function setParameter($name, $value): void; + + public function removeParameter($name): void; + + public function setParameters(array $parameters): void; + + public function createSubContext(): ExtractionContextInterface; } \ No newline at end of file diff --git a/Extraction/Extractor/Constraint/ChoiceConstraintExtractor.php b/Extraction/Extractor/Constraint/ChoiceConstraintExtractor.php index 21c0f11..9f38d25 100644 --- a/Extraction/Extractor/Constraint/ChoiceConstraintExtractor.php +++ b/Extraction/Extractor/Constraint/ChoiceConstraintExtractor.php @@ -5,6 +5,7 @@ use Draw\Swagger\Extraction\Extractor\ConstraintExtractor; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\Choice as SupportedConstraint; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; class ChoiceConstraintExtractor extends ConstraintExtractor { @@ -18,27 +19,23 @@ public function supportConstraint(Constraint $constraint) } /** - * @param SupportedConstraint $constraint + * @param SupportedConstraint|Constraint $constraint * @param ConstraintExtractionContext $context */ public function extractConstraint(Constraint $constraint, ConstraintExtractionContext $context) { $this->assertSupportConstraint($constraint); - if ($constraint->callback) { - if (is_callable(array($className, $constraint->callback))) { - $choices = call_user_func(array($className, $constraint->callback)); - } elseif (is_callable($constraint->callback)) { - $choices = call_user_func($constraint->callback); - } else { + if (!is_callable($constraint->callback)) { throw new ConstraintDefinitionException('The Choice constraint expects a valid callback'); } + $choices = call_user_func($constraint->callback); } else { $choices = $constraint->choices; } - foreach($choices as $choice) { + foreach ($choices as $choice) { $context->propertySchema->enum[] = $choice; } } diff --git a/Extraction/Extractor/Constraint/CountConstraintExtractor.php b/Extraction/Extractor/Constraint/CountConstraintExtractor.php index 939aaf7..91bfe4d 100644 --- a/Extraction/Extractor/Constraint/CountConstraintExtractor.php +++ b/Extraction/Extractor/Constraint/CountConstraintExtractor.php @@ -18,7 +18,7 @@ public function supportConstraint(Constraint $constraint) } /** - * @param SupportedConstraint $constraint + * @param SupportedConstraint|Constraint $constraint * @param ConstraintExtractionContext $context */ public function extractConstraint(Constraint $constraint, ConstraintExtractionContext $context) diff --git a/Extraction/Extractor/Constraint/LengthConstraintExtractor.php b/Extraction/Extractor/Constraint/LengthConstraintExtractor.php index e33cf68..317045e 100644 --- a/Extraction/Extractor/Constraint/LengthConstraintExtractor.php +++ b/Extraction/Extractor/Constraint/LengthConstraintExtractor.php @@ -18,7 +18,7 @@ public function supportConstraint(Constraint $constraint) } /** - * @param SupportedConstraint $constraint + * @param SupportedConstraint|Constraint $constraint * @param ConstraintExtractionContext $context */ public function extractConstraint(Constraint $constraint, ConstraintExtractionContext $context) diff --git a/Extraction/Extractor/Constraint/NotBlankConstraintExtractor.php b/Extraction/Extractor/Constraint/NotBlankConstraintExtractor.php index 4cf6408..c4d4c84 100644 --- a/Extraction/Extractor/Constraint/NotBlankConstraintExtractor.php +++ b/Extraction/Extractor/Constraint/NotBlankConstraintExtractor.php @@ -18,7 +18,7 @@ public function supportConstraint(Constraint $constraint) } /** - * @param SupportedConstraint $constraint + * @param SupportedConstraint|Constraint $constraint * @param ConstraintExtractionContext $context */ public function extractConstraint(Constraint $constraint, ConstraintExtractionContext $context) diff --git a/Extraction/Extractor/Constraint/NotNullConstraintExtractor.php b/Extraction/Extractor/Constraint/NotNullConstraintExtractor.php index 3e1ac87..4d79391 100644 --- a/Extraction/Extractor/Constraint/NotNullConstraintExtractor.php +++ b/Extraction/Extractor/Constraint/NotNullConstraintExtractor.php @@ -18,7 +18,7 @@ public function supportConstraint(Constraint $constraint) } /** - * @param SupportedConstraint $constraint + * @param SupportedConstraint|Constraint $constraint * @param ConstraintExtractionContext $context */ public function extractConstraint(Constraint $constraint, ConstraintExtractionContext $context) diff --git a/Extraction/Extractor/Constraint/RangeConstraintExtractor.php b/Extraction/Extractor/Constraint/RangeConstraintExtractor.php index 41a8496..9d74e89 100644 --- a/Extraction/Extractor/Constraint/RangeConstraintExtractor.php +++ b/Extraction/Extractor/Constraint/RangeConstraintExtractor.php @@ -18,7 +18,7 @@ public function supportConstraint(Constraint $constraint) } /** - * @param SupportedConstraint $constraint + * @param SupportedConstraint|Constraint $constraint * @param ConstraintExtractionContext $context */ public function extractConstraint(Constraint $constraint, ConstraintExtractionContext $context) diff --git a/Extraction/Extractor/ConstraintExtractor.php b/Extraction/Extractor/ConstraintExtractor.php index a16a6fa..1b92316 100644 --- a/Extraction/Extractor/ConstraintExtractor.php +++ b/Extraction/Extractor/ConstraintExtractor.php @@ -7,8 +7,10 @@ use Draw\Swagger\Extraction\Extractor\Constraint\ConstraintExtractionContext; use Draw\Swagger\Extraction\Extractor\Constraint\ConstraintExtractorInterface; use Draw\Swagger\Schema\Schema; +use InvalidArgumentException; use Symfony\Component\Validator\Constraint; use ReflectionClass; +use Symfony\Component\Validator\Mapping\ClassMetadataInterface; use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; abstract class ConstraintExtractor implements ConstraintExtractorInterface @@ -30,7 +32,7 @@ abstract public function extractConstraint(Constraint $constraint, ConstraintExt protected function assertSupportConstraint(Constraint $constraint) { if (!$this->supportConstraint($constraint)) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf( 'The constraint of type [%s] is not supported by [%s]', get_class($constraint), @@ -87,9 +89,9 @@ private function getPropertiesConstraints(ReflectionClass $reflectionClass, Sche } $constraints = array(); - $classMetadata = $this->metadataFactory->getMetadataFor($class); - /* @var \Symfony\Component\Validator\Mapping\ClassMetadataInterface $classMetadata */ + /* @var ClassMetadataInterface $classMetadata */ + $classMetadata = $this->metadataFactory->getMetadataFor($class); foreach ($classMetadata->getConstrainedProperties() as $propertyName) { //This is to prevent hading properties just because they have validation diff --git a/Extraction/Extractor/DoctrineInheritanceExtractor.php b/Extraction/Extractor/DoctrineInheritanceExtractor.php index b3e2307..f53e6c6 100644 --- a/Extraction/Extractor/DoctrineInheritanceExtractor.php +++ b/Extraction/Extractor/DoctrineInheritanceExtractor.php @@ -1,11 +1,12 @@ managerRegistry->getManagerForClass($source->name)->getClassMetadata($source->name); - if (!$metaData instanceof ClassMetadata) { + if (!$metaData instanceof ClassMetadataInfo) { return; } diff --git a/Extraction/Extractor/JmsSerializer/DynamicObjectTypeToSchemaHandler.php b/Extraction/Extractor/JmsSerializer/DynamicObjectTypeToSchemaHandler.php index 0edb0ec..7242f3e 100644 --- a/Extraction/Extractor/JmsSerializer/DynamicObjectTypeToSchemaHandler.php +++ b/Extraction/Extractor/JmsSerializer/DynamicObjectTypeToSchemaHandler.php @@ -1,7 +1,6 @@ docBlockFactory ->create( @@ -96,6 +99,7 @@ public function extract($method, $operation, ExtractionContextInterface $extract /** @var Type[] $types */ $types = []; $hasVoid = false; + $returnTag = null; foreach ($docBlock->getTagsByName('return') as $returnTag) { /* @var $returnTag DocBlock\Tags\Return_ */ $type = $returnTag->getType(); @@ -108,27 +112,28 @@ public function extract($method, $operation, ExtractionContextInterface $extract } if($hasVoid && count($types) > 1) { - throw new \RuntimeException('Operation returning [void] cannot return anything else.'); + throw new RuntimeException('Operation returning [void] cannot return anything else.'); } - foreach ($types as $type) { - $response = new Response(); - $response->description = (string)$returnTag->getDescription() ?: null; - if($type != 'void' && $type != 'null') { - $response->schema = $responseSchema = new Schema(); - $subContext = $extractionContext->createSubContext(); - $subContext->setParameter('controller-reflection-method', $method); - $subContext->setParameter('response', $response); - $extractionContext->getSwagger()->extract((string)$type, $responseSchema, $subContext); - $statusCode = $subContext->getParameter('response-status-code', 200); - } else { - $statusCode = 204; - } + if($returnTag) { + foreach ($types as $type) { + $response = new Response(); + $response->description = (string)$returnTag->getDescription() ?: null; + if($type != 'void' && $type != 'null') { + $response->schema = $responseSchema = new Schema(); + $subContext = $extractionContext->createSubContext(); + $subContext->setParameter('controller-reflection-method', $method); + $subContext->setParameter('response', $response); + $extractionContext->getSwagger()->extract((string)$type, $responseSchema, $subContext); + $statusCode = $subContext->getParameter('response-status-code', 200); + } else { + $statusCode = 204; + } - $operation->responses[$statusCode] = $response; + $operation->responses[$statusCode] = $response; + } } - if(!$operation->responses) { $operation->responses[204] = $response = new Response(); $response->description = 'Empty response mean success.'; @@ -142,7 +147,8 @@ public function extract($method, $operation, ExtractionContextInterface $extract /* @var $throwTag DocBlock\Tags\Throws */ $type = (string)$throwTag->getType(); - $exceptionClass = new \ReflectionClass((string)$type); + $exceptionClass = new ReflectionClass((string)$type); + /** @var Exception $exception */ $exception = $exceptionClass->newInstanceWithoutConstructor(); list($code, $message) = $this->getExceptionInformation($exception); $operation->responses[$code] = $exceptionResponse = new Response(); @@ -223,7 +229,7 @@ public function extract($method, $operation, ExtractionContextInterface $extract } } - private function getExceptionInformation(\Exception $exception) + private function getExceptionInformation(Exception $exception) { foreach ($this->exceptionResponseCodes as $class => $information) { if ($exception instanceof $class) { diff --git a/Extraction/Extractor/SwaggerParameterExtractor.php b/Extraction/Extractor/SwaggerParameterExtractor.php index 7a49030..284e1da 100644 --- a/Extraction/Extractor/SwaggerParameterExtractor.php +++ b/Extraction/Extractor/SwaggerParameterExtractor.php @@ -8,6 +8,7 @@ use Draw\Swagger\Extraction\ExtractorInterface; use Draw\Swagger\Schema\BaseParameter; use Draw\Swagger\Schema\Operation; +use ReflectionMethod; class SwaggerParameterExtractor implements ExtractorInterface { @@ -23,7 +24,7 @@ public function __construct(Reader $reader) public function canExtract($source, $target, ExtractionContextInterface $extractionContext) { - if(!$source instanceof \ReflectionMethod) { + if(!$source instanceof ReflectionMethod) { return false; } @@ -35,7 +36,7 @@ public function canExtract($source, $target, ExtractionContextInterface $extract } /** - * @param \ReflectionMethod $source + * @param ReflectionMethod $source * @param Operation $target * @param ExtractionContextInterface $extractionContext * @throws ExtractionImpossibleException diff --git a/Extraction/Extractor/TypeSchemaExtractor.php b/Extraction/Extractor/TypeSchemaExtractor.php index e16e814..26ff929 100644 --- a/Extraction/Extractor/TypeSchemaExtractor.php +++ b/Extraction/Extractor/TypeSchemaExtractor.php @@ -9,6 +9,7 @@ use Draw\Swagger\Schema\Schema; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\Collection; +use ReflectionClass; class TypeSchemaExtractor implements ExtractorInterface { @@ -84,7 +85,7 @@ public function extract($source, $target, ExtractionContextInterface $extraction if($target->type == 'generic') { $target->type = 'object'; - $reflectionClass = new \ReflectionClass($primitiveType['class']); + $reflectionClass = new ReflectionClass($primitiveType['class']); $subContext = $extractionContext->createSubContext(); $subContext->setParameter('generic-template', $primitiveType['template']); $extractionContext->getSwagger()->extract( @@ -98,7 +99,7 @@ public function extract($source, $target, ExtractionContextInterface $extraction if ($target->type == "object") { $target->type = null; - $reflectionClass = new \ReflectionClass($primitiveType['class']); + $reflectionClass = new ReflectionClass($primitiveType['class']); $rootSchema = $extractionContext->getRootSchema(); $context = $extractionContext->getParameter('model-context', []); diff --git a/JMSSerializerListener.php b/JMSSerializerListener.php index 2e38127..9d22e1d 100644 --- a/JMSSerializerListener.php +++ b/JMSSerializerListener.php @@ -10,6 +10,7 @@ use JMS\Serializer\EventDispatcher\PreSerializeEvent; use JMS\Serializer\JsonSerializationVisitor; use JMS\Serializer\Metadata\StaticPropertyMetadata; +use ReflectionClass; class JMSSerializerListener implements EventSubscriberInterface { @@ -69,8 +70,8 @@ public function onPreDeserialize(PreDeserializeEvent $event) return; } - $reflectionClass = new \ReflectionClass($type['name']); - if(!$reflectionClass->implementsInterface('Draw\Swagger\Schema\VendorExtensionSupportInterface')) { + $reflectionClass = new ReflectionClass($type['name']); + if(!$reflectionClass->implementsInterface(VendorExtensionSupportInterface::class)) { return; } @@ -88,7 +89,7 @@ public function onPostSerialize(ObjectEvent $event) if($object instanceof VendorExtensionSupportInterface) { foreach($object->getVendorData() as $key => $value) { //The context argument is there for older version of JMS, new version don't have a third argument - $visitor->visitProperty(new StaticPropertyMetadata("", $key, $value), $value, $event->getContext()); + $visitor->visitProperty(new StaticPropertyMetadata("", $key, $value), $value); } } } diff --git a/Swagger.php b/Swagger.php index 833136b..f16bff0 100644 --- a/Swagger.php +++ b/Swagger.php @@ -7,12 +7,14 @@ use Draw\Swagger\Extraction\ExtractionContextInterface; use Draw\Swagger\Extraction\ExtractorInterface; use Draw\Swagger\Extraction\Extractor\SwaggerSchemaExtractor; +use InvalidArgumentException; use JMS\Serializer\EventDispatcher\EventDispatcher; use JMS\Serializer\Handler\HandlerRegistry; use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerInterface; use Draw\Swagger\Schema\Swagger as Schema; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Validation; /** @@ -119,27 +121,22 @@ public function dump(Schema $schema, $validate = true) */ public function validate(Schema $schema) { - $validator = Validation::createValidatorBuilder() + /** @var ConstraintViolationList $result */ + $result = Validation::createValidatorBuilder() ->enableAnnotationMapping(new AnnotationReader()) - ->getValidator(); - - //This is to support legacy system, that way we don't we are less strict for dependencies - if ($validator instanceof \Symfony\Component\Validator\ValidatorInterface) { - $result = $validator->validate($schema, array(Constraint::DEFAULT_GROUP), true, true); - } else { - $result = $validator->validate($schema, null, array(Constraint::DEFAULT_GROUP)); - } + ->getValidator() + ->validate($schema, null, array(Constraint::DEFAULT_GROUP)); if (count($result)) { - throw new \InvalidArgumentException("" . $result); + throw new InvalidArgumentException("" . $result); } } /** - * @param mixed $source - * @param mixed $type - * @return mixed - * @api + * @param $source + * @param null $type + * @param ExtractionContextInterface|null $extractionContext + * @return Schema */ public function extract($source, $type = null, ExtractionContextInterface $extractionContext = null) { diff --git a/Tests/Extraction/Extractor/JmsExtractorTest.php b/Tests/Extraction/Extractor/JmsExtractorTest.php index 02c4b2a..afe422f 100644 --- a/Tests/Extraction/Extractor/JmsExtractorTest.php +++ b/Tests/Extraction/Extractor/JmsExtractorTest.php @@ -10,6 +10,7 @@ use JMS\Serializer\Annotation as JMS; use JMS\Serializer\Naming\CamelCaseNamingStrategy; use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; +use JMS\Serializer\SerializationContext; use JMS\Serializer\SerializerBuilder; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -35,14 +36,10 @@ public function setUp() { $serializer = SerializerBuilder::create()->build(); - //This is to be compatible for version 1,2,3 of jms since the method getMetadataFactory doesn't exists anymore - //When using the library you should inject the factory on your initializing flow - $property = new \ReflectionProperty(get_class($serializer), 'factory'); - $property->setAccessible(true); - $metadataFactory = $property->getValue($serializer); + $serializer->serialize([], 'json', $context = new SerializationContext()); $this->jmsExtractor = new JmsExtractor( - $metadataFactory, + $context->getMetadataFactory(), new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()) ); } @@ -57,7 +54,7 @@ public function setUp() public function testCanExtract($source, $type, $canBeExtract) { if (!is_null($source)) { - $source = new \ReflectionClass($source); + $source = new ReflectionClass($source); } /** @var ExtractionContextInterface $context */ diff --git a/Tests/Extraction/Extractor/PhpDocOperationExtractorTest.php b/Tests/Extraction/Extractor/PhpDocOperationExtractorTest.php index 77ee744..aa0ef0e 100644 --- a/Tests/Extraction/Extractor/PhpDocOperationExtractorTest.php +++ b/Tests/Extraction/Extractor/PhpDocOperationExtractorTest.php @@ -8,7 +8,10 @@ use Draw\Swagger\Schema\Operation; use Draw\Swagger\Schema\PathItem; use Draw\Swagger\Swagger; +use Exception; +use LengthException; use PHPUnit\Framework\TestCase; +use ReflectionMethod; class PhpDocOperationExtractorTest extends TestCase { @@ -24,7 +27,7 @@ public function setUp() public function provideTestCanExtract() { - $reflectionMethod = new \ReflectionMethod(__NAMESPACE__ . '\PhpDocOperationExtractorStubService', 'operation'); + $reflectionMethod = new ReflectionMethod(__NAMESPACE__ . '\PhpDocOperationExtractorStubService', 'operation'); return array( array(null, null, false), @@ -114,12 +117,10 @@ public function testExtract_genericCollection() /** * @param $method * @return ExtractionContext - * @throws ExtractionImpossibleException - * @throws \ReflectionException */ private function extractStubServiceMethod($method) { - $reflectionMethod = new \ReflectionMethod(__NAMESPACE__ . '\PhpDocOperationExtractorStubService', $method); + $reflectionMethod = new ReflectionMethod(__NAMESPACE__ . '\PhpDocOperationExtractorStubService', $method); $context = $this->getExtractionContext(); $context->getSwagger()->registerExtractor(new TypeSchemaExtractor()); @@ -147,6 +148,9 @@ class PhpDocOperationExtractorStubClass } +/** + * This class is a stub and the code implementation make no sens, just the doc is usefull + */ class PhpDocOperationExtractorStubService { /** @@ -156,12 +160,15 @@ class PhpDocOperationExtractorStubService * * @return PhpDocOperationExtractorStubService * - * @throws \Exception When problem occur - * @throws \LengthException - * @throws \Draw\Swagger\Extraction\ExtractionImpossibleException + * @throws Exception When problem occur + * @throws LengthException + * @throws ExtractionImpossibleException */ public function operation(PhpDocOperationExtractorStubService $service, $string, array $array) { + if($string) { + throw new ExtractionImpossibleException(); + } return $service; } @@ -186,7 +193,7 @@ public function defaultVoid() */ public function arrayOfPrimitive() { - + return []; } /** @@ -194,6 +201,6 @@ public function arrayOfPrimitive() */ public function genericCollection() { - + return new PhpDocOperationExtractorStubClass(); } } \ No newline at end of file diff --git a/Tests/Extraction/Extractor/SwaggerSchemaExtractorTest.php b/Tests/Extraction/Extractor/SwaggerSchemaExtractorTest.php index 8b9bf05..1c44e37 100644 --- a/Tests/Extraction/Extractor/SwaggerSchemaExtractorTest.php +++ b/Tests/Extraction/Extractor/SwaggerSchemaExtractorTest.php @@ -6,6 +6,7 @@ use Draw\Swagger\Schema\Swagger; use JMS\Serializer\SerializerBuilder; use PHPUnit\Framework\TestCase; +use stdClass; class SwaggerSchemaExtractorTest extends TestCase { @@ -17,7 +18,7 @@ public function provideTestCanExtract() array("{}", new Swagger(), false), array('{"swagger":"1.0"}', new Swagger(), false), array('{"swagger":"2.0"}', '', false), - array('{"swagger":"2.0"}', new \stdClass(), false) + array('{"swagger":"2.0"}', new stdClass(), false) ); } diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index 38042f4..208a677 100644 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -1,4 +1,4 @@ =7.2", "ext-json": "*", - "symfony/validator": ">=2.3", - "jms/serializer": "~1.0|~2.0|~3.0", - "phpdocumentor/reflection-docblock": "~3.0|~4.0|~5.0", - "symfony/property-info": "~4.0|~5.0" + "symfony/validator": "~3.4|~4.3|~5.0", + "jms/serializer": "~3.0", + "phpdocumentor/reflection-docblock": "~4.0|~5.0" }, "require-dev": { - "phpunit/phpunit": "~7.0" + "phpunit/phpunit": "~7.0", + "doctrine/orm": "^2.7", + "doctrine/common": "^2.12" } }