Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko authored and github-actions[bot] committed Aug 29, 2022
1 parent b41adad commit e3ade14
Show file tree
Hide file tree
Showing 21 changed files with 25 additions and 50 deletions.
6 changes: 0 additions & 6 deletions src/Support/ComplexTypeHandler/JsonResourceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@

use Dedoc\Scramble\Support\ClassAstHelper;
use Dedoc\Scramble\Support\Generator\Types\OpenApiTypeHelper;
use Dedoc\Scramble\Support\Infer\Handler\ReturnTypeGettingExtensions;
use Dedoc\Scramble\Support\ResponseExtractor\ModelInfo;
use Dedoc\Scramble\Support\Type\Identifier;
use Dedoc\Scramble\Support\Type\ObjectType;
use Dedoc\Scramble\Support\Type\Type;
use Dedoc\Scramble\Support\TypeHandlers\TypeHandlers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Str;
use PhpParser\Node\Expr\Array_;
use Symfony\Component\Yaml\Yaml;

class JsonResourceHandler
{
Expand Down
1 change: 0 additions & 1 deletion src/Support/Generator/Types/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function toArray()
$result = parent::toArray();

try {

if (count($this->properties)) {
$properties = [];
foreach ($this->properties as $name => $property) {
Expand Down
3 changes: 2 additions & 1 deletion src/Support/Generator/Types/OpenApiTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static function fromType(\Dedoc\Scramble\Support\Type\AbstractType $type)
if (! $item->isOptional) {
$requiredKeys[] = $item->key;
}

return [
$item->key => static::fromType($item),
];
Expand All @@ -86,7 +87,7 @@ public static function fromType(\Dedoc\Scramble\Support\Type\AbstractType $type)
}
} elseif ($type instanceof Union) {
if (count($type->types) === 2 && collect($type->types)->contains(fn ($t) => $t instanceof \Dedoc\Scramble\Support\Type\NullType)) {
$notNullType = collect($type->types)->first(fn ($t) => !($t instanceof \Dedoc\Scramble\Support\Type\NullType));
$notNullType = collect($type->types)->first(fn ($t) => ! ($t instanceof \Dedoc\Scramble\Support\Type\NullType));
if ($notNullType) {
$openApiType = static::fromType($notNullType)->nullable(true);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/Support/Infer/Handler/ArrayHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Dedoc\Scramble\Support\Infer\Handler;

use Dedoc\Scramble\Support\Type\ArrayItemType_;
use Dedoc\Scramble\Support\Type\ArrayType;
use Dedoc\Scramble\Support\Type\UnknownType;
use PhpParser\Node;

class ArrayHandler
Expand Down
1 change: 0 additions & 1 deletion src/Support/Infer/Handler/FunctionLikeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Dedoc\Scramble\Support\Infer\Handler;

use Dedoc\Scramble\Support\Infer\Scope\Scope;
use Dedoc\Scramble\Support\Infer\Scope\ScopeContext;
use Dedoc\Scramble\Support\Type\FunctionLikeType;
use Dedoc\Scramble\Support\Type\FunctionType;
use Dedoc\Scramble\Support\Type\TypeHelper;
Expand Down
9 changes: 0 additions & 9 deletions src/Support/Infer/Handler/NewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@

namespace Dedoc\Scramble\Support\Infer\Handler;

use Dedoc\Scramble\Support\Infer\Scope\Scope;
use Dedoc\Scramble\Support\Type\FunctionLikeType;
use Dedoc\Scramble\Support\Type\FunctionType;
use Dedoc\Scramble\Support\Type\Identifier;
use Dedoc\Scramble\Support\Type\IntegerType;
use Dedoc\Scramble\Support\Type\ObjectType;
use Dedoc\Scramble\Support\Type\StringType;
use Dedoc\Scramble\Support\Type\Union;
use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\NodeFinder;

class NewHandler
{
Expand Down
3 changes: 2 additions & 1 deletion src/Support/Infer/Handler/ReturnTypeGettingExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ReturnTypeGettingExtensions
{
static $extensions = [];
public static $extensions = [];

public function shouldHandle($node)
{
Expand All @@ -22,6 +22,7 @@ function ($acc, $extensionClass) use ($node) {
if ($type) {
return $type;
}

return $acc;
},
);
Expand Down
8 changes: 2 additions & 6 deletions src/Support/Infer/Handler/ScalarHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

namespace Dedoc\Scramble\Support\Infer\Handler;

use Dedoc\Scramble\Support\Infer\Scope\Scope;
use Dedoc\Scramble\Support\Type\FunctionLikeType;
use Dedoc\Scramble\Support\Type\FunctionType;
use Dedoc\Scramble\Support\Type\IntegerType;
use Dedoc\Scramble\Support\Type\StringType;
use Dedoc\Scramble\Support\Type\Union;
use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\NodeFinder;

class ScalarHandler
{
Expand All @@ -23,11 +17,13 @@ public function leave(Node\Scalar $node)
{
if ($node instanceof Node\Scalar\String_) {
$node->setAttribute('type', new StringType());

return;
}

if ($node instanceof Node\Scalar\LNumber) {
$node->setAttribute('type', new IntegerType());

return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Infer/Scope/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(ScopeContext $context, callable $namesResolver, ?Sco

public function isInClass()
{
return !!$this->context->class;
return (bool) $this->context->class;
}

public function class(): ?ObjectType
Expand Down
1 change: 1 addition & 0 deletions src/Support/Infer/Scope/ScopeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ScopeContext
public function setClass(?ObjectType $class): ScopeContext
{
$this->class = $class;

return $this;
}
}
1 change: 1 addition & 0 deletions src/Support/Infer/TypeInferringVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private function getOrCreateScope()
if (! isset($this->scope)) {
$this->scope = new Scope(new ScopeContext, $this->namesResolver);
}

return $this->scope;
}
}
13 changes: 6 additions & 7 deletions src/Support/InferExtensions/JsonResourceTypeInfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class JsonResourceTypeInfer
{
static $jsonResourcesModelTypesCache = [];
public static $jsonResourcesModelTypesCache = [];

public function getNodeReturnType(Node $node, Scope $scope)
{
Expand All @@ -43,28 +43,28 @@ public function getNodeReturnType(Node $node, Scope $scope)
* $this->mergeWhen()
*/
if ($this->isMethodCallToThis($node, ['merge', 'mergeWhen'])) {
if(! $type = $node->args[count($node->args) - 1]->value->getAttribute('type') ?? null) {
if (! $type = $node->args[count($node->args) - 1]->value->getAttribute('type') ?? null) {
return null;
}

if ($type instanceof FunctionType) {
$type = $type->getReturnType();
}

return (new Generic(
return new Generic(
new Identifier(MergeValue::class),
[
$node->name->name === 'merge' ? new LiteralBooleanType(true) : new BooleanType(),
$type
$type,
],
));
);
}

/*
* $this->when()
*/
if ($this->isMethodCallToThis($node, ['when'])) {
if(! $type = $node->args[count($node->args) - 1]->value->getAttribute('type') ?? null) {
if (! $type = $node->args[count($node->args) - 1]->value->getAttribute('type') ?? null) {
return null;
}

Expand Down Expand Up @@ -115,7 +115,6 @@ private static function modelType(ObjectType $jsonClass, Scope $scope): Type
return static::$jsonResourcesModelTypesCache[$jsonClass->name] = $modelType;
}


private static function getModelName(string $jsonResourceClassName, \ReflectionClass $reflectionClass, callable $getFqName)
{
$phpDoc = $reflectionClass->getDocComment() ?: '';
Expand Down
5 changes: 1 addition & 4 deletions src/Support/ResponseExtractor/ResponsesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Dedoc\Scramble\Support\Generator\Response;
use Dedoc\Scramble\Support\Generator\Schema;
use Dedoc\Scramble\Support\RouteInfo;
use Dedoc\Scramble\Support\Type\Generic;
use Dedoc\Scramble\Support\Type\Type;
use Dedoc\Scramble\Support\TypeHandlers\TypeHandlers;
use Illuminate\Support\Arr;

class ResponsesExtractor
Expand All @@ -24,7 +21,7 @@ public function __construct(RouteInfo $routeInfo)
public function __invoke()
{
return collect(Arr::wrap([$this->routeInfo->getHandledReturnType()]))
->filter(fn ($t) => !! $t[1])
->filter(fn ($t) => (bool) $t[1])
->map(function ($type) {
$type = $type[1];

Expand Down
2 changes: 0 additions & 2 deletions src/Support/RouteInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Dedoc\Scramble\Support\ComplexTypeHandler\ComplexTypeHandlers;
use Dedoc\Scramble\Support\Infer\TypeInferringVisitor;
use Dedoc\Scramble\Support\Type\FunctionLikeType;
use Dedoc\Scramble\Support\Type\Identifier;
use Dedoc\Scramble\Support\TypeHandlers\PhpDocTypeWalker;
use Dedoc\Scramble\Support\TypeHandlers\ResolveFqnPhpDocTypeVisitor;
use Dedoc\Scramble\Support\TypeHandlers\TypeHandlers;
Expand Down Expand Up @@ -85,7 +84,6 @@ public function phpDoc(): PhpDocNode
}
}


return $this->phpDoc;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Support/Type/ArrayItemType_.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ class ArrayItemType_ extends AbstractType
{
/** @var string|int|null */
public $key;

public Type $value;

public bool $isOptional;

public function __construct(
$key,
Type $value,
bool $isOptional = false
)
{
) {
$this->key = $key;
$this->value = $value;
$this->isOptional = $isOptional;
Expand Down
1 change: 1 addition & 0 deletions src/Support/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function isSame(Type $type)
public function toString(): string
{
$numIndex = 0;

return sprintf(
'array{%s}',
implode(', ', array_map(function (ArrayItemType_ $item) use (&$numIndex) {
Expand Down
1 change: 1 addition & 0 deletions src/Support/Type/FunctionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
public function setReturnType(Type $type): self
{
$this->returnType = $type;

return $this;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Support/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class ObjectType extends AbstractType
public function __construct(
string $name,
array $properties = []
)
{
) {
$this->name = $name;
$this->properties = $properties;
}
Expand Down
1 change: 0 additions & 1 deletion src/Support/TypeHandlers/PhpDocTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Dedoc\Scramble\Support\TypeHandlers;

use Dedoc\Scramble\Support\Type\ArrayType;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
Expand Down
5 changes: 2 additions & 3 deletions tests/InferTypesTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

use Dedoc\Scramble\Support\ClassAstHelper;
use Dedoc\Scramble\Support\Generator\Types\OpenApiTypeHelper;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Resources\Json\JsonResource;
use PhpParser\Node\Stmt\ClassMethod;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Spatie\Snapshots\assertMatchesTextSnapshot;

uses(RefreshDatabase::class);
Expand All @@ -28,7 +27,7 @@ class InferTypesTest_SampleClass
{
public function wow($request)
{
return (new BrandEdge($request));
return new BrandEdge($request);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function getEnvironmentSetUp($app)

protected function defineDatabaseMigrations()
{
$this->loadMigrationsFrom(__DIR__ . '/migrations');
$this->loadMigrationsFrom(__DIR__.'/migrations');
}
}

0 comments on commit e3ade14

Please sign in to comment.