Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"phpdocumentor/reflection-docblock": "^5.0"
},
"require-dev": {
"laravel/pint": "^1.24",
"orchestra/testbench": "^5.3|^6.0|^7.0|^9.0|^10.0",
"phpunit/phpunit": "^9.5.13|^10.5|^11.5"
},
Expand Down
35 changes: 17 additions & 18 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
shortenArraysForExportThreshold="10"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="Laravel OpenAPI Test Suite">
<directory suffix="Test.php">./tests</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
3 changes: 3 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "laravel"
}
4 changes: 3 additions & 1 deletion src/Attributes/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
class Extension
{
public ?string $factory;

public ?string $key;

public ?string $value;

public function __construct(string $factory = null, string $key = null, string $value = null)
public function __construct(?string $factory = null, ?string $key = null, ?string $value = null)
{
if ($factory) {
$this->factory = class_exists($factory) ? $factory : app()->getNamespace().'OpenApi\\Extensions\\'.$factory;
Expand Down
17 changes: 10 additions & 7 deletions src/Attributes/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@ class Operation
public ?array $servers;

/**
* @param string|null $id
* @param array $tags
* @param \Vyuldashev\LaravelOpenApi\Factories\SecuritySchemeFactory|string|null $security
* @param string|null $method
*
* @throws InvalidArgumentException
*/
public function __construct(string $id = null, array $tags = [], string $security = null, string $method = null, array $servers = null)
{
public function __construct(
?string $id = null,
array $tags = [],
?string $security = null,
?string $method = null,
?array $servers = null
) {
$this->id = $id;
$this->tags = $tags;
$this->method = $method;
$this->servers = $servers;

if ($security === '') {
//user wants to turn off security on this operation
// user wants to turn off security on this operation
$this->security = $security;

return;
Expand All @@ -47,7 +49,8 @@ public function __construct(string $id = null, array $tags = [], string $securit

if (! is_a($this->security, SecuritySchemeFactory::class, true)) {
throw new InvalidArgumentException(
sprintf('Security class is either not declared or is not an instance of %s', SecuritySchemeFactory::class)
sprintf('Security class is either not declared or is not an instance of %s',
SecuritySchemeFactory::class)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Attributes/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Response

public ?string $description;

public function __construct(string $factory, int $statusCode = null, string $description = null)
public function __construct(string $factory, ?int $statusCode = null, ?string $description = null)
{
$this->factory = class_exists($factory) ? $factory : app()->getNamespace().'OpenApi\\Responses\\'.$factory;

Expand Down
4 changes: 4 additions & 0 deletions src/Builders/ComponentsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
class ComponentsBuilder
{
protected CallbacksBuilder $callbacksBuilder;

protected RequestBodiesBuilder $requestBodiesBuilder;

protected ResponsesBuilder $responsesBuilder;

protected SchemasBuilder $schemasBuilder;

protected SecuritySchemesBuilder $securitySchemesBuilder;

public function __construct(
Expand Down
6 changes: 4 additions & 2 deletions src/Builders/Paths/Operation/ParametersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected function buildPath(RouteInformation $route): Collection

/** @var ReflectionParameter|null $reflectionParameter */
$reflectionParameter = collect($route->actionParameters)
->first(static fn (ReflectionParameter $reflectionParameter) => $reflectionParameter->name === $parameter['name']);
->first(static fn (ReflectionParameter $reflectionParameter
) => $reflectionParameter->name === $parameter['name']);

if ($reflectionParameter) {
// The reflected param has no type, so ignore (should be defined in a ParametersFactory instead)
Expand All @@ -49,7 +50,8 @@ protected function buildPath(RouteInformation $route): Collection
}
/** @var Param $description */
$description = collect($route->actionDocBlock->getTagsByName('param'))
->first(static fn (Param $param) => Str::snake($param->getVariableName()) === Str::snake($parameter['name']));
->first(static fn (Param $param
) => Str::snake($param->getVariableName()) === Str::snake($parameter['name']));

return Parameter::path()->name($parameter['name'])
->required()
Expand Down
3 changes: 2 additions & 1 deletion src/Builders/Paths/Operation/RequestBodyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class RequestBodyBuilder
public function build(RouteInformation $route): ?RequestBody
{
/** @var RequestBodyAttribute|null $requestBody */
$requestBody = $route->actionAttributes->first(static fn (object $attribute) => $attribute instanceof RequestBodyAttribute);
$requestBody = $route->actionAttributes->first(static fn (object $attribute
) => $attribute instanceof RequestBodyAttribute);

if ($requestBody) {
/** @var RequestBodyFactory $requestBodyFactory */
Expand Down
27 changes: 15 additions & 12 deletions src/Builders/Paths/OperationsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@
class OperationsBuilder
{
protected CallbacksBuilder $callbacksBuilder;

protected ParametersBuilder $parametersBuilder;

protected RequestBodyBuilder $requestBodyBuilder;

protected ResponsesBuilder $responsesBuilder;

protected ExtensionsBuilder $extensionsBuilder;

protected SecurityBuilder $securityBuilder;

public function __construct(
CallbacksBuilder $callbacksBuilder,
ParametersBuilder $parametersBuilder,
CallbacksBuilder $callbacksBuilder,
ParametersBuilder $parametersBuilder,
RequestBodyBuilder $requestBodyBuilder,
ResponsesBuilder $responsesBuilder,
ExtensionsBuilder $extensionsBuilder,
SecurityBuilder $securityBuilder
)
{
ResponsesBuilder $responsesBuilder,
ExtensionsBuilder $extensionsBuilder,
SecurityBuilder $securityBuilder
) {
$this->callbacksBuilder = $callbacksBuilder;
$this->parametersBuilder = $parametersBuilder;
$this->requestBodyBuilder = $requestBodyBuilder;
Expand All @@ -44,8 +48,7 @@ public function __construct(
}

/**
* @param RouteInformation[]|Collection $routes
* @return array
* @param RouteInformation[]|Collection $routes
*
* @throws InvalidArgumentException
*/
Expand All @@ -57,13 +60,13 @@ public function build(array|Collection $routes): array
foreach ($routes as $route) {
/** @var OperationAttribute|null $operationAttribute */
$operationAttribute = $route->actionAttributes
->first(static fn(object $attribute) => $attribute instanceof OperationAttribute);
->first(static fn (object $attribute) => $attribute instanceof OperationAttribute);

$operationId = optional($operationAttribute)->id;
$tags = $operationAttribute->tags ?? [];
$servers = collect($operationAttribute->servers)
->filter(fn($server) => app($server) instanceof ServerFactory)
->map(static fn($server) => app($server)->build())
->filter(fn ($server) => app($server) instanceof ServerFactory)
->map(static fn ($server) => app($server)->build())
->toArray();

$parameters = $this->parametersBuilder->build($route);
Expand Down
2 changes: 0 additions & 2 deletions src/Builders/PathsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public function __construct(
}

/**
* @param string $collection
* @param PathMiddleware[] $middlewares
* @return array
*/
public function build(
string $collection,
Expand Down
1 change: 0 additions & 1 deletion src/Builders/ServersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class ServersBuilder
{
/**
* @param array $config
* @return Server[]
*/
public function build(array $config): array
Expand Down
1 change: 0 additions & 1 deletion src/Builders/TagsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class TagsBuilder
{
/**
* @param array $config
* @return Tag[]
*/
public function build(array $config): array
Expand Down
8 changes: 4 additions & 4 deletions src/ClassMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function createMap(Iterator|string $dir): array

$path = $file->getRealPath() ?: $file->getPathname();

if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) {
if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
continue;
}

Expand Down Expand Up @@ -97,7 +97,7 @@ private static function findClasses(string $path): array
break;
}

if (T_DOUBLE_COLON === $tokens[$j][0]) {
if ($tokens[$j][0] === T_DOUBLE_COLON) {
$isClassConstant = true;
break;
}
Expand All @@ -114,9 +114,9 @@ private static function findClasses(string $path): array
// Find the classname
while (isset($tokens[++$i][1])) {
$t = $tokens[$i];
if (T_STRING === $t[0]) {
if ($t[0] === T_STRING) {
$class .= $t[1];
} elseif ('' !== $class && T_WHITESPACE === $t[0]) {
} elseif ($class !== '' && $t[0] === T_WHITESPACE) {
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Console/CallbackFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
class CallbackFactoryMakeCommand extends GeneratorCommand
{
protected $name = 'openapi:make-callback';

protected $description = 'Create a new callback factory class';

protected $type = 'Extension';

protected function getStub(): string
Expand Down
5 changes: 4 additions & 1 deletion src/Console/ExtensionFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
class ExtensionFactoryMakeCommand extends GeneratorCommand
{
protected $name = 'openapi:make-extension';

protected $description = 'Create a new extension factory class';

protected $type = 'Extension';

protected function buildClass($name)
{
$output = parent::buildClass($name);
$output = str_replace('DummyExtension', Str::start(Str::snake(Str::replaceLast('DummyExtension', '', class_basename($name)), '-'), 'x-'), $output);
$output = str_replace('DummyExtension',
Str::start(Str::snake(Str::replaceLast('DummyExtension', '', class_basename($name)), '-'), 'x-'), $output);

return $output;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Console/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class GenerateCommand extends Command
{
protected $signature = 'openapi:generate {collection=default} {--output= : Output file}';

protected $description = 'Generate OpenAPI specification';

public function handle(Generator $generator): void
Expand All @@ -23,8 +24,9 @@ public function handle(Generator $generator): void
}

if ($this->option('output')) {
//create file if not exists, or overwrite if exists and put the generated JSON there
file_put_contents($this->option('output'), $generator->generate($this->argument('collection'))->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
// create file if not exists, or overwrite if exists and put the generated JSON there
file_put_contents($this->option('output'),
$generator->generate($this->argument('collection'))->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

$this->info('OpenAPI specification generated successfully.');

Expand Down
2 changes: 2 additions & 0 deletions src/Console/ParametersFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
class ParametersFactoryMakeCommand extends GeneratorCommand
{
protected $name = 'openapi:make-parameters';

protected $description = 'Create a new Parameters factory class';

protected $type = 'Parameters';

protected function getStub(): string
Expand Down
2 changes: 2 additions & 0 deletions src/Console/RequestBodyFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
class RequestBodyFactoryMakeCommand extends GeneratorCommand
{
protected $name = 'openapi:make-requestbody';

protected $description = 'Create a new RequestBody factory class';

protected $type = 'RequestBody';

protected function getStub(): string
Expand Down
2 changes: 2 additions & 0 deletions src/Console/ResponseFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
class ResponseFactoryMakeCommand extends GeneratorCommand
{
protected $name = 'openapi:make-response';

protected $description = 'Create a new Response factory class';

protected $type = 'Response';

protected function getStub(): string
Expand Down
1 change: 1 addition & 0 deletions src/Console/RoutesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class RoutesCommand extends Command
{
protected $signature = 'openapi:routes';

protected $description = 'List all registered route with additional information';

public function handle(): void
Expand Down
8 changes: 6 additions & 2 deletions src/Console/SchemaFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
class SchemaFactoryMakeCommand extends GeneratorCommand
{
protected $name = 'openapi:make-schema';

protected $description = 'Create a new Schema factory class';

protected $type = 'Schema';

protected function buildClass($name)
Expand Down Expand Up @@ -46,7 +48,8 @@ protected function buildModel($output, $model)
/** @var Model $model */
$model = app($model);

$columns = SchemaFacade::connection($model->getConnectionName())->getColumnListing(config('database.connections.'.config('database.default').'.prefix', '').$model->getTable());
$columns = SchemaFacade::connection($model->getConnectionName())->getColumnListing(config('database.connections.'.config('database.default').'.prefix',
'').$model->getTable());
$connection = $model->getConnection();

$definition = 'return Schema::object(\''.class_basename($model).'\')'.PHP_EOL;
Expand All @@ -55,7 +58,8 @@ protected function buildModel($output, $model)
$properties = collect($columns)
->map(static function ($column) use ($model, $connection) {
/** @var Column $column */
$column = $connection->getDoctrineColumn(config('database.connections.'.config('database.default').'.prefix', '').$model->getTable(), $column);
$column = $connection->getDoctrineColumn(config('database.connections.'.config('database.default').'.prefix',
'').$model->getTable(), $column);
$name = $column->getName();
$default = $column->getDefault();
$notNull = $column->getNotnull();
Expand Down
Loading