Skip to content

Commit af5995a

Browse files
authored
Merge pull request #1 from Basster/feature/http-stuff
support status code and headers for JsonSerializeResponse as well.
2 parents c43abff + a82858b commit af5995a

13 files changed

+172
-35
lines changed

.php_cs.dist.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
'no_alternative_syntax' => true,
7272
'php_unit_set_up_tear_down_visibility' => true,
7373
'phpdoc_trim_consecutive_blank_line_separation' => true,
74-
'no_superfluous_phpdoc_tags' => true,
7574
'return_assignment' => true,
7675
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
7776
'php_unit_test_annotation' => ['style' => 'annotation'],

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ before_script:
1313
- phpenv config-add travis.php.ini
1414
- composer update --prefer-source -o --no-interaction
1515

16-
script: vendor/bin/phpunit -c phpunit.xml.dist
16+
script:
17+
- composer run-script psalm
18+
- vendor/bin/phpunit -c phpunit.xml.dist

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@
3131
"symfony/routing": "^4.3",
3232
"symfony/event-dispatcher": "^4.3",
3333
"symfony/http-kernel": "^4.3",
34-
"friendsofphp/php-cs-fixer": "^2.15"
34+
"friendsofphp/php-cs-fixer": "^2.15",
35+
"vimeo/psalm": "^3.5"
3536
},
3637
"suggest": {
3738
"symfony/event-dispatcher": "Required when using any of the event handlers.",
3839
"symfony/serializer": "Required when using the \\Basster\\LazyResponseBundle\\Response\\JsonSerializeResponse",
3940
"twig/twig": "Required when using the \\Basster\\LazyResponseBundle\\Response\\TwigResponse",
4041
"symfony/routing": "Required when using the \\Basster\\LazyResponseBundle\\Response\\RedirectResponse"
41-
}
42+
},
43+
"scripts":{
44+
"fix": "php-cs-fixer fix --config .php_cs.dist.php",
45+
"psalm": "psalm"
46+
}
4247
}

psalm.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
totallyTyped="false"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
</projectFiles>
15+
16+
<issueHandlers>
17+
<LessSpecificReturnType errorLevel="info" />
18+
19+
<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->
20+
21+
<DeprecatedMethod errorLevel="info" />
22+
<DeprecatedProperty errorLevel="info" />
23+
<DeprecatedClass errorLevel="info" />
24+
<DeprecatedConstant errorLevel="info" />
25+
<DeprecatedFunction errorLevel="info" />
26+
<DeprecatedInterface errorLevel="info" />
27+
<DeprecatedTrait errorLevel="info" />
28+
29+
<InternalMethod errorLevel="info" />
30+
<InternalProperty errorLevel="info" />
31+
<InternalClass errorLevel="info" />
32+
33+
<MissingClosureReturnType errorLevel="info" />
34+
<MissingReturnType errorLevel="info" />
35+
<MissingPropertyType errorLevel="info" />
36+
<InvalidDocblock errorLevel="info" />
37+
<MisplacedRequiredParam errorLevel="info" />
38+
39+
<PropertyNotSetInConstructor errorLevel="info" />
40+
<MissingConstructor errorLevel="info" />
41+
<MissingClosureParamType errorLevel="info" />
42+
<MissingParamType errorLevel="info" />
43+
44+
<RedundantCondition errorLevel="info" />
45+
46+
<DocblockTypeContradiction errorLevel="info" />
47+
<RedundantConditionGivenDocblockType errorLevel="info" />
48+
49+
<UnresolvableInclude errorLevel="info" />
50+
51+
<RawObjectIteration errorLevel="info" />
52+
53+
<InvalidStringClass errorLevel="info" />
54+
</issueHandlers>
55+
</psalm>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Basster\LazyResponseBundle\Response;
5+
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
/**
9+
* Class AbstractHttpResponse.
10+
*/
11+
abstract class AbstractLazyHttpResponse implements LazyResponseInterface
12+
{
13+
/**
14+
* @var int
15+
*/
16+
protected $status;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected $headers;
22+
23+
/**
24+
* AbstractLazyHttpResponse constructor.
25+
*/
26+
public function __construct(int $status = Response::HTTP_OK, array $headers = [])
27+
{
28+
$this->status = $status;
29+
$this->headers = $headers;
30+
}
31+
32+
public function getStatus(): int
33+
{
34+
return $this->status;
35+
}
36+
37+
public function getHeaders(): array
38+
{
39+
return $this->headers;
40+
}
41+
}

src/Response/Handler/AbstractLazyResponseHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ abstract protected function isSupported(LazyResponseInterface $controllerResult)
3333

3434
abstract protected function generateResponse(LazyResponseInterface $controllerResult): Response;
3535

36+
/**
37+
* @param mixed $controllerResult
38+
*
39+
* @return bool
40+
*/
3641
private function isSupportedLazyResponse($controllerResult): bool
3742
{
3843
return $controllerResult instanceof LazyResponseInterface && $this->isSupported($controllerResult);

src/Response/Handler/JsonSerializeResponseHandler.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ public function __construct(SerializerInterface $serializer)
2525
}
2626

2727
/**
28-
* @param JsonSerializeResponse|LazyResponseInterface $controllerResult
28+
* @param JsonSerializeResponse $controllerResult
29+
*
30+
* @psalm-suppress MoreSpecificImplementedParamType
2931
*/
3032
protected function generateResponse(LazyResponseInterface $controllerResult): Response
3133
{
32-
return new JsonResponse($this->serializer->serialize($controllerResult->getData(), 'json'));
34+
return new JsonResponse(
35+
$this->serializer->serialize($controllerResult->getData(), 'json'),
36+
$controllerResult->getStatus(),
37+
$controllerResult->getHeaders(),
38+
);
3339
}
3440

3541
protected function isSupported(LazyResponseInterface $controllerResult): bool

src/Response/Handler/RedirectResponseHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ protected function isSupported(LazyResponseInterface $controllerResult): bool
3030
}
3131

3232
/**
33-
* @param LazyResponseInterface|RedirectResponse $controllerResult
33+
* @param RedirectResponse $controllerResult
34+
*
35+
* @psalm-suppress MoreSpecificImplementedParamType
3436
*/
3537
protected function generateResponse(LazyResponseInterface $controllerResult): Response
3638
{
3739
return new SymfonyRedirectResponse(
3840
$this->router->generate(
3941
$controllerResult->getRouteName(),
4042
$controllerResult->getRouteParams()
41-
),
43+
),
4244
$controllerResult->getStatusCode(),
4345
$controllerResult->getHeaders()
4446
);

src/Response/Handler/TwigResponseHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ protected function isSupported(LazyResponseInterface $controllerResult): bool
2929
}
3030

3131
/**
32-
* @param LazyResponseInterface|TemplateResponse $controllerResult
32+
* @param TemplateResponse $controllerResult
33+
*
34+
* @psalm-suppress MoreSpecificImplementedParamType
3335
*
3436
* @throws \Twig\Error\LoaderError
3537
* @throws \Twig\Error\RuntimeError

src/Response/JsonSerializeResponse.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,32 @@
33

44
namespace Basster\LazyResponseBundle\Response;
55

6+
use Symfony\Component\HttpFoundation\Response;
7+
68
/**
79
* Class JsonSerializeResponse.
810
*/
9-
final class JsonSerializeResponse implements LazyResponseInterface
11+
final class JsonSerializeResponse extends AbstractLazyHttpResponse
1012
{
1113
/** @var mixed */
1214
private $data;
1315

14-
public function __construct($data)
16+
/**
17+
* JsonSerializeResponse constructor.
18+
*
19+
* @param mixed $data
20+
* @param int $status
21+
* @param array $headers
22+
*/
23+
public function __construct($data, int $status = Response::HTTP_OK, array $headers = [])
1524
{
25+
parent::__construct($status, $headers);
1626
$this->data = $data;
1727
}
1828

29+
/**
30+
* @return mixed
31+
*/
1932
public function getData()
2033
{
2134
return $this->data;

0 commit comments

Comments
 (0)