Skip to content

Commit

Permalink
~ cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanmac committed Dec 12, 2023
1 parent e62610e commit 03d5641
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 175 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
"doctrine/dbal": "^3.4",
"doctrine/doctrine-bundle": "^1.9 || ^2.0",
"dragonmantank/cron-expression": "^2.2 || ^3.0",
"phpdocumentor/reflection-docblock": "^5.3",
"phpdocumentor/type-resolver": "^1.7",
"symfony/config": "^4.4 || ^5.4 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0",
"symfony/form": "^4.4 || ^5.4 || ^6.0",
Expand All @@ -27,6 +25,7 @@
},
"require-dev": {
"matthiasnoback/symfony-dependency-injection-test": "^4.3",
"phpdocumentor/reflection-docblock": "^5.3",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16.1",
"roave/security-advisories": "dev-latest",
Expand Down
78 changes: 17 additions & 61 deletions src/Form/TypeGuesser/CronExpressionTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
namespace Setono\CronExpressionBundle\Form\TypeGuesser;

use Cron\CronExpression;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\Types\ContextFactory;
use phpDocumentor\Reflection\Types\Object_;
use Setono\CronExpressionBundle\Form\Type\CronExpressionType;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type;

Expand All @@ -23,7 +22,7 @@ final class CronExpressionTypeGuesser implements FormTypeGuesserInterface

public function __construct(?PropertyTypeExtractorInterface $extractor = null)
{
$this->extractor = $extractor;
$this->extractor = $extractor ?? $this->createExtractor();
}
/**
* @param string $class
Expand All @@ -35,53 +34,15 @@ public function guessType($class, $property): ?TypeGuess
return null;
}

if ($this->extractor) {
$types = $this->extractor->getTypes($class, $property);
if (!$types) {
return null;
}
foreach ($types as $type) {
if (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType() &&
CronExpression::class === $type->getClassName()) {
return $this->getGuess();
}
}
} else {
// NEXT MAJOR: Remove Compatible Layer
try {
$reflectionClass = new \ReflectionClass($class);
$reflectionProperty = $reflectionClass->getProperty($property);
if ($reflectionProperty->hasType()) {
$type = $reflectionProperty->getType();

if ($type instanceof \ReflectionNamedType) {
if ($type->getName() === CronExpression::class) {
return $this->getGuess();
}
}
} else {
$docBlockFactory = DocBlockFactory::createInstance();
$contextFactory = new ContextFactory();

$docBlock = $docBlockFactory->create($reflectionProperty,
$contextFactory->createFromReflector($reflectionProperty));

/**
* @var Var_ $tag
*/
foreach ($docBlock->getTagsByName('var') as $tag) {
$tagType = $tag->getType();
if ($tagType instanceof Object_) {
$fqsen = $tagType->getFqsen();
$fqsen = ltrim((string)$fqsen, '\\');
if (CronExpression::class === $fqsen) {
return $this->getGuess();
}
}
}
}
} catch (\ReflectionException $e) {
return null;
$types = $this->extractor->getTypes($class, $property);
if (!$types) {
return null;
}
foreach ($types as $type) {
if (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType() &&
CronExpression::class === $type->getClassName()) {
return new TypeGuess(CronExpressionType::class, [], Guess::VERY_HIGH_CONFIDENCE);
}
}
return null;
Expand Down Expand Up @@ -114,16 +75,11 @@ public function guessPattern($class, $property): ?ValueGuess
return null;
}

/**
* @param PropertyTypeExtractorInterface|null $extractor
*/
public function setExtractor(?PropertyTypeExtractorInterface $extractor): void
{
$this->extractor = $extractor;
}

protected function getGuess(): TypeGuess
private function createExtractor(): PropertyTypeExtractorInterface
{
return new TypeGuess(CronExpressionType::class, [], Guess::VERY_HIGH_CONFIDENCE);
return new PropertyInfoExtractor([], [
new PhpDocExtractor(),
new ReflectionExtractor(),
]);
}
}
4 changes: 1 addition & 3 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
<service id="setono_cron_expression.form.type_guesser.cron_expression"
class="Setono\CronExpressionBundle\Form\TypeGuesser\CronExpressionTypeGuesser">
<tag name="form.type_guesser"/>
<call method="setExtractor">
<argument type="service" id="property_info"/>
</call>
<argument type="service" id="property_info"/>
</service>
</services>
</container>
9 changes: 0 additions & 9 deletions tests/Form/TypeGuesser/CronExpressionTypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,14 @@
use Setono\CronExpressionBundle\Form\TypeGuesser\CronExpressionTypeGuesser;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;

final class CronExpressionTypeGuesserTest extends TestCase
{
private CronExpressionTypeGuesser $typeGuesser;

public function setUp(): void
{
$extractor = new PropertyInfoExtractor([], [
new PhpDocExtractor(),
new ReflectionExtractor(),
]);

$this->typeGuesser = new CronExpressionTypeGuesser();
$this->typeGuesser->setExtractor($extractor);
}

/**
Expand Down

This file was deleted.

0 comments on commit 03d5641

Please sign in to comment.