Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Setup php-cs-fixer #49

Merged
merged 4 commits into from
Dec 14, 2023
Merged
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
40 changes: 40 additions & 0 deletions .github/workflows/lint-php-cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This workflow is provided via the organization template repository
#
# https://github.com/nextcloud/.github
# https://docs.github.com/en/actions/learn-github-actions/sharing-workflows-with-your-organization

name: Lint php-cs

on: pull_request

permissions:
contents: read

concurrency:
group: lint-php-cs-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest

name: php-cs

steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Set up php8.2
uses: shivammathur/setup-php@e6f75134d35752277f093989e72e140eaa222f35 # v2
with:
php-version: 8.2
coverage: none
ini-file: development
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: composer i

- name: Lint
run: composer run cs:check || ( echo 'Please run `composer run cs:fix` to format your code' && exit 1 )
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

vendor/

testdata/
.php-cs-fixer.cache
16 changes: 16 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

require_once './vendor/autoload.php';

use Nextcloud\CodingStandard\Config;

$config = new Config();
$config
->getFinder()
->ignoreVCSIgnored(true)
->notPath('vendor')
->in(__DIR__)
->append(['generate-spec', 'merge-specs']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added those 2 files (not picked up by default due to missing .php)

return $config;
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"adhocore/cli": "^v1.6",
"phpstan/phpdoc-parser": "^1.23"
},
"require-dev": {
"nextcloud/coding-standard": "^1.1"
},
"bin": [
"generate-spec",
"merge-specs"
Expand All @@ -15,5 +18,9 @@
"psr-4": {
"OpenAPIExtractor\\": "src"
}
},
"scripts": {
"cs:check": "php-cs-fixer fix --dry-run --diff",
"cs:fix": "php-cs-fixer fix"
}
}
101 changes: 98 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ foreach ($capabilitiesFiles as $path) {
* @var Class_ $node
*/
foreach ($nodeFinder->findInstanceOf(loadAST($path), Class_::class) as $node) {
$implementsCapability = count(array_filter($node->implements, fn(Name $name) => $name->getLast() == "ICapability")) > 0;
$implementsPublicCapability = count(array_filter($node->implements, fn(Name $name) => $name->getLast() == "IPublicCapability")) > 0;
$implementsCapability = count(array_filter($node->implements, fn (Name $name) => $name->getLast() == "ICapability")) > 0;
$implementsPublicCapability = count(array_filter($node->implements, fn (Name $name) => $name->getLast() == "IPublicCapability")) > 0;
if (!$implementsCapability && !$implementsPublicCapability) {
continue;
}
Expand Down Expand Up @@ -280,9 +280,9 @@ foreach ($parsedRoutes as $key => $value) {
continue;
}

$tagName = implode("_", array_map(fn(string $s) => strtolower($s), Helpers::splitOnUppercaseFollowedByNonUppercase($controllerName)));
$tagName = implode("_", array_map(fn (string $s) => strtolower($s), Helpers::splitOnUppercaseFollowedByNonUppercase($controllerName)));
$doc = $controllerClass->getDocComment()?->getText();
if ($doc != null && count(array_filter($tags, fn(array $tag) => $tag["name"] == $tagName)) == 0) {
if ($doc != null && count(array_filter($tags, fn (array $tag) => $tag["name"] == $tagName)) == 0) {
$classDescription = [];

$docNodes = $phpDocParser->parse(new TokenIterator($lexer->tokenize($doc)))->children;
Expand Down Expand Up @@ -373,8 +373,8 @@ foreach ($parsedRoutes as $key => $value) {
}
}

$docStatusCodes = array_map(fn(ControllerMethodResponse $response) => $response->statusCode, array_filter($classMethodInfo->responses, fn(?ControllerMethodResponse $response) => $response != null));
$missingDocStatusCodes = array_unique(array_filter(array_diff($codeStatusCodes, $docStatusCodes), fn(int $code) => $code < 500));
$docStatusCodes = array_map(fn (ControllerMethodResponse $response) => $response->statusCode, array_filter($classMethodInfo->responses, fn (?ControllerMethodResponse $response) => $response != null));
$missingDocStatusCodes = array_unique(array_filter(array_diff($codeStatusCodes, $docStatusCodes), fn (int $code) => $code < 500));

if (count($missingDocStatusCodes) > 0) {
Logger::error($routeName, "Returns undocumented status codes: " . implode(", ", $missingDocStatusCodes));
Expand Down Expand Up @@ -415,7 +415,7 @@ foreach ($routes as $route) {
$urlParameters = [];

preg_match_all("/{[^}]*}/", $route->url, $urlParameters);
$urlParameters = array_map(fn(string $name) => substr($name, 1, -1), $urlParameters[0]);
$urlParameters = array_map(fn (string $name) => substr($name, 1, -1), $urlParameters[0]);

foreach ($urlParameters as $urlParameter) {
$matchingParameters = array_filter($route->controllerMethod->parameters, function (ControllerMethodParameter $param) use ($urlParameter) {
Expand Down Expand Up @@ -462,7 +462,7 @@ foreach ($routes as $route) {
}
$schema["enum"] = $enum;
$schema["default"] = end($enum);
} else if ($requirement != null) {
} elseif ($requirement != null) {
$schema["pattern"] = $requirement;
}
}
Expand Down Expand Up @@ -499,25 +499,25 @@ foreach ($routes as $route) {
}

$mergedResponses = [];
foreach (array_unique(array_map(fn(ControllerMethodResponse $response) => $response->statusCode, array_filter($route->controllerMethod->responses, fn(?ControllerMethodResponse $response) => $response != null))) as $statusCode) {
foreach (array_unique(array_map(fn (ControllerMethodResponse $response) => $response->statusCode, array_filter($route->controllerMethod->responses, fn (?ControllerMethodResponse $response) => $response != null))) as $statusCode) {
if ($firstStatusCode && count($mergedResponses) > 0) {
break;
}

$statusCodeResponses = array_filter($route->controllerMethod->responses, fn(?ControllerMethodResponse $response) => $response != null && $response->statusCode == $statusCode);
$headers = array_merge(...array_map(fn(ControllerMethodResponse $response) => $response->headers ?? [], $statusCodeResponses));
$statusCodeResponses = array_filter($route->controllerMethod->responses, fn (?ControllerMethodResponse $response) => $response != null && $response->statusCode == $statusCode);
$headers = array_merge(...array_map(fn (ControllerMethodResponse $response) => $response->headers ?? [], $statusCodeResponses));

$mergedContentTypeResponses = [];
foreach (array_unique(array_map(fn(ControllerMethodResponse $response) => $response->contentType, array_filter($statusCodeResponses, fn(ControllerMethodResponse $response) => $response->contentType != null))) as $contentType) {
foreach (array_unique(array_map(fn (ControllerMethodResponse $response) => $response->contentType, array_filter($statusCodeResponses, fn (ControllerMethodResponse $response) => $response->contentType != null))) as $contentType) {
if ($firstContentType && count($mergedContentTypeResponses) > 0) {
break;
}

/** @var ControllerMethodResponse[] $contentTypeResponses */
$contentTypeResponses = array_values(array_filter($statusCodeResponses, fn(ControllerMethodResponse $response) => $response->contentType == $contentType));
$contentTypeResponses = array_values(array_filter($statusCodeResponses, fn (ControllerMethodResponse $response) => $response->contentType == $contentType));

$hasEmpty = count(array_filter($contentTypeResponses, fn(ControllerMethodResponse $response) => $response->type == null)) > 0;
$uniqueResponses = array_values(array_intersect_key($contentTypeResponses, array_unique(array_map(fn(ControllerMethodResponse $response) => $response->type->toArray($openapiVersion), array_filter($contentTypeResponses, fn(ControllerMethodResponse $response) => $response->type != null)), SORT_REGULAR)));
$hasEmpty = count(array_filter($contentTypeResponses, fn (ControllerMethodResponse $response) => $response->type == null)) > 0;
$uniqueResponses = array_values(array_intersect_key($contentTypeResponses, array_unique(array_map(fn (ControllerMethodResponse $response) => $response->type->toArray($openapiVersion), array_filter($contentTypeResponses, fn (ControllerMethodResponse $response) => $response->type != null)), SORT_REGULAR)));
if (count($uniqueResponses) == 1) {
if ($hasEmpty) {
$mergedContentTypeResponses[$contentType] = [];
Expand Down Expand Up @@ -545,7 +545,7 @@ foreach ($routes as $route) {
"headers" => array_combine(
array_keys($headers),
array_map(
fn(OpenApiType $type) => [
fn (OpenApiType $type) => [
"schema" => $type->toArray($openapiVersion),
],
array_values($headers),
Expand All @@ -559,7 +559,7 @@ foreach ($routes as $route) {
}

$operationId = [$route->tag];
$operationId = array_merge($operationId, array_map(fn(string $s) => Helpers::mapVerb(strtolower($s)), Helpers::splitOnUppercaseFollowedByNonUppercase($route->methodName)));
$operationId = array_merge($operationId, array_map(fn (string $s) => Helpers::mapVerb(strtolower($s)), Helpers::splitOnUppercaseFollowedByNonUppercase($route->methodName)));
if ($route->postfix != null) {
$operationId[] = $route->postfix;
}
Expand Down Expand Up @@ -587,7 +587,7 @@ foreach ($routes as $route) {
count($security) > 0 ? ["security" => $security] : [],
count($queryParameters) > 0 || count($pathParameters) > 0 || $route->isOCS ? [
"parameters" => array_merge(
array_map(fn(ControllerMethodParameter $parameter) => array_merge(
array_map(fn (ControllerMethodParameter $parameter) => array_merge(
[
"name" => $parameter->name . ($parameter->type->type == "array" ? "[]" : ""),
"in" => "query",
Expand Down
6 changes: 3 additions & 3 deletions merge-specs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ $data
["properties"]
["capabilities"]
["anyOf"]
= array_map(fn(string $capability) => ["\$ref" => "#/components/schemas/" . $capability], $capabilities);
= array_map(fn (string $capability) => ["\$ref" => "#/components/schemas/" . $capability], $capabilities);

function loadSpec(string $path): array {
return rewriteRefs(json_decode(file_get_contents($path), true));
Expand Down Expand Up @@ -121,7 +121,7 @@ function rewriteSchemaNames(array $spec): array {
$schemas = $spec["components"]["schemas"];
$readableAppID = Helpers::generateReadableAppID($spec["info"]["title"]);
return array_combine(
array_map(fn(string $key) => $key == "OCSMeta" ? $key : $readableAppID . $key, array_keys($schemas)),
array_map(fn (string $key) => $key == "OCSMeta" ? $key : $readableAppID . $key, array_keys($schemas)),
array_values($schemas),
);
}
Expand All @@ -146,7 +146,7 @@ function rewriteOperations(array $spec): array {
$operation["operationId"] = $spec["info"]["title"] . "-" . $operation["operationId"];
}
if (array_key_exists("tags", $operation)) {
$operation["tags"] = array_map(fn(string $tag) => $spec["info"]["title"] . "/" . $tag, $operation["tags"]);
$operation["tags"] = array_map(fn (string $tag) => $spec["info"]["title"] . "/" . $tag, $operation["tags"]);
} else {
$operation["tags"] = [$spec["info"]["title"]];
}
Expand Down
Loading