Skip to content

Commit 793e711

Browse files
authored
Setup PHP-CS-Fixer + GHA workflow (Case 130695) (#3)
* Setup PHP-CS-Fixer + GHA workflow (Case 130695) * Fix CS with PHP-CS-Fixer 2.19.0 Co-authored-by: mpdude <[email protected]>
1 parent 31eb59d commit 793e711

File tree

9 files changed

+102
-57
lines changed

9 files changed

+102
-57
lines changed

.github/workflows/fix-cs-php.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Update this by running
2+
# curl https://gist.githubusercontent.com/mpdude/ca93a185bcbf56eb7e341632ad4f8263/raw/fix-cs-php.yml > .github/workflows/fix-cs-php.yml
3+
4+
on:
5+
push:
6+
branches:
7+
- master
8+
pull_request:
9+
10+
name: Coding Standards
11+
12+
jobs:
13+
open-pr-for-cs-violations:
14+
name: PHP-CS-Fixer
15+
runs-on: ubuntu-20.04
16+
if: github.actor != 'dependabot[bot]'
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
with:
21+
ref: ${{ github.head_ref }}
22+
23+
- name: Run PHP-CS-Fixer
24+
uses: docker://oskarstark/php-cs-fixer-ga:2.19.0
25+
26+
- name: Commit and push back changes
27+
uses: stefanzweifel/git-auto-commit-action@v4
28+
with:
29+
commit_message: "Fix CS with PHP-CS-Fixer 2.19.0"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
vendor/
2+
.php_cs.cache

.php_cs.dist

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
return PhpCsFixer\Config::create()
4+
->setRules([
5+
'@Symfony' => true,
6+
'array_syntax' => array('syntax' => 'short'),
7+
'no_unreachable_default_argument_value' => false,
8+
'braces' => array('allow_single_line_closure' => true),
9+
'heredoc_to_nowdoc' => false,
10+
'phpdoc_annotation_without_dot' => false,
11+
'php_unit_test_annotation' => ['style' => 'annotation'],
12+
'php_unit_method_casing' => false,
13+
'global_namespace_import' => ['import_classes' => true, 'import_constants' => false, 'import_functions' => false],
14+
])
15+
->setRiskyAllowed(true)
16+
->setFinder(
17+
PhpCsFixer\Finder::create()
18+
->in(__DIR__)
19+
->notPath('conf/')
20+
->notPath('tmp/')
21+
->notPath('node_modules/')
22+
->notPath('var/cache')
23+
->notPath('vendor/')
24+
)
25+
;

DependencyInjection/WebfactoryHttpCacheExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class WebfactoryHttpCacheExtension extends Extension
1414
{
1515
public function load(array $configs, ContainerBuilder $container)
1616
{
17-
$locator = new FileLocator(__DIR__ . '/../NotModified');
17+
$locator = new FileLocator(__DIR__.'/../NotModified');
1818
$yamlLoader = new XmlFileLoader($container, $locator);
1919
$yamlLoader->load('services.xml');
2020
}

NotModified/Annotation/ReplaceWithNotModifiedResponse.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Webfactory\HttpCacheBundle\NotModified\Annotation;
1111

12+
use DateTime;
13+
use RuntimeException;
1214
use Symfony\Component\DependencyInjection\ContainerInterface;
1315
use Symfony\Component\HttpFoundation\Request;
1416
use Webfactory\HttpCacheBundle\NotModified\LastModifiedDeterminator;
@@ -31,57 +33,50 @@ final class ReplaceWithNotModifiedResponse
3133
/** @var ContainerInterface */
3234
private $container;
3335

34-
/** @var \DateTime|null */
36+
/** @var DateTime|null */
3537
private $lastModified;
3638

37-
/**
38-
* @param array $parameters
39-
*/
4039
public function __construct(array $parameters)
4140
{
4241
$this->parameters = $parameters;
4342
}
4443

4544
/**
46-
* @param Request $request
47-
* @return \DateTime|null
45+
* @return DateTime|null
4846
*/
4947
public function determineLastModified(Request $request)
5048
{
5149
$this->initialiseLastModifiedDeterminators();
5250

5351
foreach ($this->lastModifiedDeterminators as $lastModifiedDeterminator) {
5452
$lastModifiedOfCurrentDeterminator = $lastModifiedDeterminator->getLastModified($request);
55-
if ($this->lastModified === null || $this->lastModified < $lastModifiedOfCurrentDeterminator) {
53+
if (null === $this->lastModified || $this->lastModified < $lastModifiedOfCurrentDeterminator) {
5654
$this->lastModified = $lastModifiedOfCurrentDeterminator;
5755
}
5856
}
5957

6058
return $this->lastModified;
6159
}
6260

63-
/**
64-
* @param ContainerInterface $container
65-
*/
6661
public function setContainer(ContainerInterface $container)
6762
{
6863
$this->container = $container;
6964
}
7065

7166
private function initialiseLastModifiedDeterminators()
7267
{
73-
if (count($this->parameters['value']) === 0) {
74-
throw new \RuntimeException('The annotation ' . get_class($this) . ' has to be parametrised with LastModifiedDeterminators.');
68+
if (0 === count($this->parameters['value'])) {
69+
throw new RuntimeException('The annotation '.get_class($this).' has to be parametrised with LastModifiedDeterminators.');
7570
}
7671

7772
foreach ($this->parameters['value'] as $lastModifiedDeterminatorDescription) {
7873
$lastModifiedDeterminator = null;
7974

8075
if (is_string($lastModifiedDeterminatorDescription)) {
81-
if ($lastModifiedDeterminatorDescription[0] === '@') {
76+
if ('@' === $lastModifiedDeterminatorDescription[0]) {
8277
$lastModifiedDeterminator = $this->container->get(substr($lastModifiedDeterminatorDescription, 1));
8378
} else {
84-
$lastModifiedDeterminator = new $lastModifiedDeterminatorDescription;
79+
$lastModifiedDeterminator = new $lastModifiedDeterminatorDescription();
8580
}
8681
}
8782

@@ -92,9 +87,7 @@ private function initialiseLastModifiedDeterminators()
9287
}
9388

9489
if (!($lastModifiedDeterminator instanceof LastModifiedDeterminator)) {
95-
throw new \RuntimeException(
96-
'The class "' . get_class($lastModifiedDeterminator) . '" does not implement ' . LastModifiedDeterminator::class . '.'
97-
);
90+
throw new RuntimeException('The class "'.get_class($lastModifiedDeterminator).'" does not implement '.LastModifiedDeterminator::class.'.');
9891
}
9992

10093
$this->lastModifiedDeterminators[] = $lastModifiedDeterminator;

NotModified/EventListener.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace Webfactory\HttpCacheBundle\NotModified;
1111

1212
use Doctrine\Common\Annotations\Reader;
13+
use ReflectionMethod;
14+
use SplObjectStorage;
1315
use Symfony\Component\DependencyInjection\ContainerInterface;
1416
use Symfony\Component\HttpFoundation\Response;
1517
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
@@ -33,7 +35,7 @@ final class EventListener
3335
* Maps (master and sub) requests to their corresponding last modified date. This date is determined by the
3436
* ReplaceWithNotModifiedResponse annotation of the corresponding controller's action.
3537
*
36-
* @var \SplObjectStorage
38+
* @var SplObjectStorage
3739
*/
3840
private $lastModified;
3941

@@ -42,15 +44,11 @@ final class EventListener
4244
*/
4345
private $debug;
4446

45-
/**
46-
* @param Reader $reader
47-
* @param ContainerInterface $container
48-
*/
4947
public function __construct(Reader $reader, ContainerInterface $container, bool $debug = false)
5048
{
5149
$this->reader = $reader;
5250
$this->container = $container;
53-
$this->lastModified = new \SplObjectStorage();
51+
$this->lastModified = new SplObjectStorage();
5452
$this->debug = $debug;
5553
}
5654

@@ -59,8 +57,6 @@ public function __construct(Reader $reader, ContainerInterface $container, bool
5957
* If it determines that the underlying resources for the response were not modified after the "If-Modified-Since"
6058
* header in the request, replace the determined controller action with a minimal action that just returns an
6159
* "empty" response with a 304 Not Modified HTTP status code.
62-
*
63-
* @param FilterControllerEvent $event
6460
*/
6561
public function onKernelController(FilterControllerEvent $event)
6662
{
@@ -97,8 +93,6 @@ public function onKernelController(FilterControllerEvent $event)
9793
/**
9894
* If a last modified date was determined for the current (master or sub) request, set it to the response so the
9995
* client can use it for the "If-Modified-Since" header in subsequent requests.
100-
*
101-
* @param FilterResponseEvent $event
10296
*/
10397
public function onKernelResponse(FilterResponseEvent $event)
10498
{
@@ -112,6 +106,7 @@ public function onKernelResponse(FilterResponseEvent $event)
112106

113107
/**
114108
* @param $controllerCallable callable PHP callback pointing to the method to reflect on.
109+
*
115110
* @return ReplaceWithNotModifiedResponse|null The annotation, if found. Null otherwise.
116111
*/
117112
private function findAnnotation(callable $controllerCallable)
@@ -121,10 +116,11 @@ private function findAnnotation(callable $controllerCallable)
121116
}
122117

123118
[$class, $methodName] = $controllerCallable;
124-
$method = new \ReflectionMethod($class, $methodName);
119+
$method = new ReflectionMethod($class, $methodName);
125120

126121
/** @var ReplaceWithNotModifiedResponse|null $annotation */
127122
$annotation = $this->reader->getMethodAnnotation($method, ReplaceWithNotModifiedResponse::class);
123+
128124
return $annotation;
129125
}
130126
}

NotModified/LastModifiedDeterminator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Webfactory\HttpCacheBundle\NotModified;
1111

12+
use DateTime;
1213
use Symfony\Component\HttpFoundation\Request;
1314

1415
/**
@@ -21,8 +22,7 @@
2122
interface LastModifiedDeterminator
2223
{
2324
/**
24-
* @param Request $request
25-
* @return \DateTime|null
25+
* @return DateTime|null
2626
*/
2727
public function getLastModified(Request $request);
2828
}

Tests/NotModified/Annotation/ReplaceWithNotModifiedResponseTest.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
namespace Webfactory\HttpCacheBundle\Tests\NotModified\Annotation;
1111

12+
use DateTime;
13+
use PHPUnit_Framework_MockObject_MockObject;
14+
use PHPUnit_Framework_TestCase;
15+
use RuntimeException;
1216
use Symfony\Component\DependencyInjection\ContainerInterface;
1317
use Symfony\Component\HttpFoundation\Request;
1418
use Webfactory\HttpCacheBundle\NotModified\Annotation\ReplaceWithNotModifiedResponse;
@@ -17,14 +21,14 @@
1721
/**
1822
* Tests for the ReplaceWithNotModifiedResponse annotation.
1923
*/
20-
final class ReplaceWithNotModifiedResponseTest extends \PHPUnit_Framework_TestCase
24+
final class ReplaceWithNotModifiedResponseTest extends PHPUnit_Framework_TestCase
2125
{
2226
/**
2327
* @test
2428
*/
2529
public function lastModifiedDescriptionsCannotBeEmpty()
2630
{
27-
$this->setExpectedException(\RuntimeException::class);
31+
$this->setExpectedException(RuntimeException::class);
2832
$annotation = new ReplaceWithNotModifiedResponse(['value' => []]);
2933
$annotation->determineLastModified(new Request());
3034
}
@@ -44,7 +48,7 @@ public function stringAsSimpleLastModifiedDescription()
4448
*/
4549
public function serviceNameAsLastModifiedDescription()
4650
{
47-
/** @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject $container */
51+
/** @var ContainerInterface|PHPUnit_Framework_MockObject_MockObject $container */
4852
$container = $this->getMock(ContainerInterface::class);
4953
$container->expects($this->once())
5054
->method('get')
@@ -64,7 +68,7 @@ public function serviceNameAsLastModifiedDescription()
6468
public function arrayAslastModifiedDeterminatorDescriptionWithConstructorArguments()
6569
{
6670
$this->setExpectedException(null);
67-
$annotation = new ReplaceWithNotModifiedResponse(['value' => [[MyLastModifedDeterminator::class => new \DateTime('2000-01-01')]]]);
71+
$annotation = new ReplaceWithNotModifiedResponse(['value' => [[MyLastModifedDeterminator::class => new DateTime('2000-01-01')]]]);
6872
$annotation->determineLastModified(new Request());
6973
}
7074

@@ -73,7 +77,7 @@ public function arrayAslastModifiedDeterminatorDescriptionWithConstructorArgumen
7377
*/
7478
public function lastModifiedDeterminatorsHaveToImplementInterface()
7579
{
76-
$this->setExpectedException(\RuntimeException::class);
80+
$this->setExpectedException(RuntimeException::class);
7781
$annotation = new ReplaceWithNotModifiedResponse(['value' => [FakeLastModifiedDeterminatorWithoutInterface::class]]);
7882
$annotation->determineLastModified(new Request());
7983
}
@@ -85,7 +89,7 @@ public function determineLastModifiedDeterminesLastModifiedOfOneDeterminator()
8589
{
8690
$annotation = new ReplaceWithNotModifiedResponse(['value' => [MyLastModifedDeterminator::class]]);
8791
$this->assertEquals(
88-
new \DateTime(),
92+
new DateTime(),
8993
$annotation->determineLastModified(new Request()),
9094
'',
9195
$allowedDeltaInSeconds = 3
@@ -98,28 +102,26 @@ public function determineLastModifiedDeterminesLastModifiedOfOneDeterminator()
98102
public function determineLastModifiedDeterminesLastModifiedOfMultipleDeterminators()
99103
{
100104
$annotation = new ReplaceWithNotModifiedResponse(['value' => [
101-
[MyLastModifedDeterminator::class => new \DateTime('2001-01-01')],
102-
[MyLastModifedDeterminator::class => new \DateTime('2003-01-01')],
103-
[MyLastModifedDeterminator::class => new \DateTime('2002-01-01')],
105+
[MyLastModifedDeterminator::class => new DateTime('2001-01-01')],
106+
[MyLastModifedDeterminator::class => new DateTime('2003-01-01')],
107+
[MyLastModifedDeterminator::class => new DateTime('2002-01-01')],
104108
]]);
105-
$this->assertEquals(new \DateTime('2003-01-01'), $annotation->determineLastModified(new Request()));
109+
$this->assertEquals(new DateTime('2003-01-01'), $annotation->determineLastModified(new Request()));
106110
}
107111
}
108112

109-
110-
111113
final class FakeLastModifiedDeterminatorWithoutInterface
112114
{
113115
}
114116

115117
final class MyLastModifedDeterminator implements LastModifiedDeterminator
116118
{
117-
/** @var \DateTime */
119+
/** @var DateTime */
118120
private $lastModified;
119121

120-
public function __construct(\DateTime $lastModified = null)
122+
public function __construct(DateTime $lastModified = null)
121123
{
122-
$this->lastModified = $lastModified ?: new \DateTime();
124+
$this->lastModified = $lastModified ?: new DateTime();
123125
}
124126

125127
public function getLastModified(Request $request)

0 commit comments

Comments
 (0)