Skip to content

Commit

Permalink
clean code and fixe some dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Mar 24, 2020
1 parent 501102a commit b723f40
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 155 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 6 additions & 5 deletions Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
19 changes: 8 additions & 11 deletions Extraction/ExtractionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand Down
62 changes: 17 additions & 45 deletions Extraction/ExtractionContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 5 additions & 8 deletions Extraction/Extractor/Constraint/ChoiceConstraintExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions Extraction/Extractor/ConstraintExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions Extraction/Extractor/DoctrineInheritanceExtractor.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php namespace Draw\Swagger\Extraction\Extractor;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Persistence\ManagerRegistry;
use Draw\Swagger\Extraction\ExtractionContextInterface;
use Draw\Swagger\Extraction\ExtractionImpossibleException;
use Draw\Swagger\Extraction\ExtractorInterface;
use Draw\Swagger\Schema\Schema;
use ReflectionClass;

class DoctrineInheritanceExtractor implements ExtractorInterface
{
Expand All @@ -21,7 +22,7 @@ public function __construct(ManagerRegistry $managerRegistry)

public function canExtract($source, $target, ExtractionContextInterface $extractionContext)
{
if (!$source instanceof \ReflectionClass) {
if (!$source instanceof ReflectionClass) {
return false;
}

Expand All @@ -37,7 +38,7 @@ public function canExtract($source, $target, ExtractionContextInterface $extract
}

/**
* @param \ReflectionClass $source
* @param ReflectionClass $source
* @param Schema $target
* @param ExtractionContextInterface $extractionContext
*
Expand All @@ -49,9 +50,8 @@ public function extract($source, $target, ExtractionContextInterface $extraction
throw new ExtractionImpossibleException();
}


$metaData = $this->managerRegistry->getManagerForClass($source->name)->getClassMetadata($source->name);
if (!$metaData instanceof ClassMetadata) {
if (!$metaData instanceof ClassMetadataInfo) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace Draw\Swagger\Extraction\Extractor\JmsSerializer;

use Draw\Swagger\Extraction\ExtractionContextInterface;
use Draw\Swagger\Extraction\Extractor\JmsExtractor;
use Draw\Swagger\Schema\Schema;
use JMS\Serializer\Metadata\PropertyMetadata;

Expand Down
Loading

0 comments on commit b723f40

Please sign in to comment.