Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #29 from Purple-Booth-Ltd/follow-coding-standard
Browse files Browse the repository at this point in the history
Follow PSR2
  • Loading branch information
joelwurtz authored Sep 8, 2016
2 parents a4c2f2f + d80a57a commit 2389e29
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 30 deletions.
8 changes: 8 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Needed to get styleci-bridge loaded
require_once __DIR__.'/vendor/sllh/php-cs-fixer-styleci-bridge/autoload.php';

use SLLH\StyleCIBridge\ConfigBridge;

return ConfigBridge::create();
8 changes: 8 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
preset: psr2

finder:
exclude:
- "tests/fixtures"
- "tests/fixtures-boilerplate"
- "generated"
- "bin"
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"require-dev": {
"phpunit/phpunit": "^4.7",
"friendsofphp/php-cs-fixer": "v2.0.0-alpha"
"friendsofphp/php-cs-fixer": "v2.0.0-alpha",
"sllh/php-cs-fixer-styleci-bridge": "^2.1"
},
"conflict": {
"friendsofphp/php-cs-fixer": "<2.0.0-alpha|>2.0.0-alpha"
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/GeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class GeneratorFactory
{
static public function build()
public static function build()
{
if (class_exists('PhpParser\ParserFactory')) {
$parserFactory = new ParserFactory();
Expand Down
28 changes: 15 additions & 13 deletions src/Generator/InputGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ protected function createHeaderStatements(Operation $operation, $queryParamVaria

$headers = [
new Expr\ArrayItem(
new Scalar\String_($operation->getHost()), new Scalar\String_('Host')
new Scalar\String_($operation->getHost()),
new Scalar\String_('Host')
),
];

Expand All @@ -272,32 +273,33 @@ protected function createHeaderStatements(Operation $operation, $queryParamVaria
if ($produces && in_array("application/json", $produces)) {
$headers[]
= new Expr\ArrayItem(
new Expr\Array_(
[
new Expr\Array_(
[
new Expr\ArrayItem(
new Scalar\String_("application/json")
),
]
),
new Scalar\String_('Accept')
);
]
),
new Scalar\String_('Accept')
);
}

$consumes = $operation->getOperation()->getProduces();

if ($operation->getOperation()->getParameters() && $consumes) {
$bodyParameters = array_filter(
$operation->getOperation()->getParameters(), function ($parameter) {
return $parameter instanceof BodyParameter;
}
$operation->getOperation()->getParameters(),
function ($parameter) {
return $parameter instanceof BodyParameter;
}
);

if (count($bodyParameters) > 0 && in_array("application/json", $consumes)) {
$headers[]
= new Expr\ArrayItem(
new Scalar\String_("application/json"),
new Scalar\String_('Content-Type')
);
new Scalar\String_("application/json"),
new Scalar\String_('Content-Type')
);
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/Generator/OperationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function generate($name, Operation $operation, Context $context)
// $request = $this->messageFactory->createRequest('method', $url, $headers, $body);
new Expr\Assign(new Expr\Variable('request'), new Expr\MethodCall(
new Expr\PropertyFetch(new Expr\Variable('this'), 'messageFactory'),
'createRequest', [
'createRequest',
[
new Arg(new Scalar\String_($operation->getMethod())),
new Arg($urlVariable),
new Arg($headerVariable),
Expand All @@ -75,7 +76,8 @@ public function generate($name, Operation $operation, Context $context)
)),
// if ($fetch === self::FETCH_PROMISE) { return $promise }
new Stmt\If_(
new Expr\BinaryOp\Identical(new Expr\ConstFetch(new Name('self::FETCH_PROMISE')), new Expr\Variable('fetch')), [
new Expr\BinaryOp\Identical(new Expr\ConstFetch(new Name('self::FETCH_PROMISE')), new Expr\Variable('fetch')),
[
'stmts' => [
new Stmt\Return_(new Expr\Variable('promise'))
]
Expand Down Expand Up @@ -110,15 +112,17 @@ public function generate($name, Operation $operation, Context $context)

if (!empty($outputStatements)) {
$statements[] = new Stmt\If_(
new Expr\BinaryOp\Equal(new Expr\ConstFetch(new Name('self::FETCH_OBJECT')), new Expr\Variable('fetch')), [
new Expr\BinaryOp\Equal(new Expr\ConstFetch(new Name('self::FETCH_OBJECT')), new Expr\Variable('fetch')),
[
'stmts' => $outputStatements
]
);
}

// return $response
$statements[] = new Stmt\Return_(new Expr\Variable('response'));
$documentation = array_merge([
$documentation = array_merge(
[
'/**',
sprintf(" * %s", $operation->getOperation()->getDescription()),
' *',
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/OutputGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ protected function createResponseDenormalizationStatement($status, $schema, Cont
new Expr\BinaryOp\Equal(
new Scalar\String_($status),
new Expr\MethodCall(new Expr\Variable('response'), 'getStatusCode')
), [
),
[
'stmts' => [
new Stmt\Return_(new Expr\MethodCall(
new Expr\PropertyFetch(new Expr\Variable('this'), 'serializer'),
Expand Down
1 change: 0 additions & 1 deletion src/Generator/Parameter/FormDataParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ public function generateQueryParamStatements($parameter, Expr $queryParamVariabl

return $statements;
}

}
3 changes: 1 addition & 2 deletions src/JaneOpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ public function __construct(
ClientGenerator $clientGenerator,
PrettyPrinterAbstract $prettyPrinter,
ConfigInterface $fixerConfig = null
)
{
) {
$this->schemaParser = $schemaParser;
$this->clientGenerator = $clientGenerator;
$this->prettyPrinter = $prettyPrinter;
Expand Down
10 changes: 6 additions & 4 deletions src/Naming/OperationUrlNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public function generateFunctionName(Operation $operation)
$part = $matches['part'][$index];

if (preg_match_all('/{(?P<parameter>[^{}]+)}/', $part, $parameterMatches)) {
foreach($parameterMatches[0] as $parameterIndex => $parameterMatch) {
foreach ($parameterMatches[0] as $parameterIndex => $parameterMatch) {
$withoutSnakes = preg_replace_callback(
'/(^|_|\.)+(.)/', function ($match) {
return ('.' === $match[1] ? '_' : '') . strtoupper($match[2]);
}, $parameterMatches['parameter'][ $parameterIndex ]
'/(^|_|\.)+(.)/',
function ($match) {
return ('.' === $match[1] ? '_' : '') . strtoupper($match[2]);
},
$parameterMatches['parameter'][ $parameterIndex ]
);

$methodNameParts[] = 'By' . ucfirst($withoutSnakes);
Expand Down
9 changes: 6 additions & 3 deletions src/SchemaParser/SchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,20 @@ public function parseSchema($openApiSpec)

try {
$schema = $this->serializer->deserialize(
$openApiSpecContents, $schemaClass, self::CONTENT_TYPE_JSON
$openApiSpecContents,
$schemaClass,
self::CONTENT_TYPE_JSON
);

} catch (\Exception $exception) {
$jsonException = $exception;
}

if (!$schema) {
try {
$schema = $this->serializer->deserialize(
$openApiSpecContents, $schemaClass, self::CONTENT_TYPE_YAML
$openApiSpecContents,
$schemaClass,
self::CONTENT_TYPE_YAML
);
} catch (\Exception $exception) {
$yamlException = $exception;
Expand Down

0 comments on commit 2389e29

Please sign in to comment.