Skip to content

Commit 089c738

Browse files
authored
use latest PHP 8.0 features (#432)
* use latest PHP 8.0 features * fix tests
1 parent 0bd566a commit 089c738

20 files changed

+316
-295
lines changed

Command/DumpCommand.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the FOSJsRoutingBundle package.
57
*
@@ -76,28 +78,31 @@ protected function configure(): void
7678
null,
7779
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
7880
'Specify expose domain',
79-
array()
81+
[]
8082
)
8183
;
8284
}
8385

8486
protected function execute(InputInterface $input, OutputInterface $output): int
8587
{
86-
if(!in_array($input->getOption('format'), array('js', 'json'))) {
88+
if (!in_array($input->getOption('format'), ['js', 'json'])) {
8789
$output->writeln('<error>Invalid format specified. Use js or json.</error>');
90+
8891
return 1;
8992
}
9093

9194
$callback = $input->getOption('callback');
92-
if(empty($callback)) {
95+
if (empty($callback)) {
9396
$output->writeln('<error>If you include --callback it must not be empty. Do you perhaps want --format=json</error>');
97+
9498
return 1;
9599
}
96100

97101
$output->writeln('Dumping exposed routes.');
98102
$output->writeln('');
99103

100104
$this->doDump($input, $output);
105+
101106
return 0;
102107
}
103108

@@ -114,28 +119,28 @@ private function doDump(InputInterface $input, OutputInterface $output): void
114119
sprintf(
115120
'%s/web/js/fos_js_routes%s.%s',
116121
$this->projectDir,
117-
empty($domain) ? '' : ('_' . implode('_', $domain)),
122+
empty($domain) ? '' : ('_'.implode('_', $domain)),
118123
$input->getOption('format')
119124
);
120-
125+
121126
if (!is_dir($dir = dirname($targetPath))) {
122-
$output->writeln('<info>[dir+]</info> ' . $dir);
127+
$output->writeln('<info>[dir+]</info> '.$dir);
123128
if (false === @mkdir($dir, 0777, true)) {
124-
throw new \RuntimeException('Unable to create directory ' . $dir);
129+
throw new \RuntimeException('Unable to create directory '.$dir);
125130
}
126131
}
127132

128-
$output->writeln('<info>[file+]</info> ' . $targetPath);
133+
$output->writeln('<info>[file+]</info> '.$targetPath);
129134

130135
$baseUrl = null !== $this->requestContextBaseUrl ?
131136
$this->requestContextBaseUrl :
132137
$this->extractor->getBaseUrl()
133138
;
134139

135140
if ($input->getOption('pretty-print')) {
136-
$params = array('json_encode_options' => JSON_PRETTY_PRINT);
141+
$params = ['json_encode_options' => JSON_PRETTY_PRINT];
137142
} else {
138-
$params = array();
143+
$params = [];
139144
}
140145

141146
$content = $serializer->serialize(
@@ -153,12 +158,12 @@ private function doDump(InputInterface $input, OutputInterface $output): void
153158
$params
154159
);
155160

156-
if('js' == $input->getOption('format')) {
157-
$content = sprintf("%s(%s);", $input->getOption('callback'), $content);
161+
if ('js' == $input->getOption('format')) {
162+
$content = sprintf('%s(%s);', $input->getOption('callback'), $content);
158163
}
159164

160165
if (false === @file_put_contents($targetPath, $content)) {
161-
throw new \RuntimeException('Unable to write file ' . $targetPath);
166+
throw new \RuntimeException('Unable to write file '.$targetPath);
162167
}
163168
}
164169
}

Command/RouterDebugExposedCommand.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the FOSJsRoutingBundle package.
57
*
@@ -19,8 +21,8 @@
1921
use Symfony\Component\Console\Input\InputOption;
2022
use Symfony\Component\Console\Output\OutputInterface;
2123
use Symfony\Component\Routing\Route;
22-
use Symfony\Component\Routing\RouterInterface;
2324
use Symfony\Component\Routing\RouteCollection;
25+
use Symfony\Component\Routing\RouterInterface;
2426

2527
/**
2628
* A console command for retrieving information about exposed routes.
@@ -36,20 +38,19 @@ public function __construct(private ExposedRoutesExtractorInterface $extractor,
3638
parent::__construct();
3739
}
3840

39-
4041
/**
4142
* {@inheritdoc}
4243
*/
4344
protected function configure(): void
4445
{
4546
$this
46-
->setDefinition(array(
47+
->setDefinition([
4748
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
4849
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
4950
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
5051
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
51-
new InputOption('domain', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify expose domain', array())
52-
))
52+
new InputOption('domain', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify expose domain', []),
53+
])
5354
->setName('fos:js-routing:debug')
5455
->setDescription('Displays currently exposed routes for an application')
5556
->setHelp(<<<EOF
@@ -81,23 +82,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8182
}
8283

8384
$helper = new DescriptorHelper();
84-
$helper->describe($output, $route, array(
85-
'format' => $input->getOption('format'),
86-
'raw_text' => $input->getOption('raw'),
85+
$helper->describe($output, $route, [
86+
'format' => $input->getOption('format'),
87+
'raw_text' => $input->getOption('raw'),
8788
'show_controllers' => $input->getOption('show-controllers'),
88-
));
89+
]);
8990
} else {
9091
$helper = new DescriptorHelper();
91-
$helper->describe($output, $this->getRoutes($input->getOption('domain')), array(
92-
'format' => $input->getOption('format'),
93-
'raw_text' => $input->getOption('raw'),
92+
$helper->describe($output, $this->getRoutes($input->getOption('domain')), [
93+
'format' => $input->getOption('format'),
94+
'raw_text' => $input->getOption('raw'),
9495
'show_controllers' => $input->getOption('show-controllers'),
95-
));
96+
]);
9697
}
98+
9799
return 0;
98100
}
99101

100-
protected function getRoutes($domain = array()): RouteCollection
102+
protected function getRoutes($domain = []): RouteCollection
101103
{
102104
$routes = $this->extractor->getRoutes();
103105

@@ -108,14 +110,12 @@ protected function getRoutes($domain = array()): RouteCollection
108110
$targetRoutes = new RouteCollection();
109111

110112
foreach ($routes as $name => $route) {
111-
112113
$expose = $route->getOption('expose');
113-
$expose = is_string($expose) ? ($expose === 'true' ? 'default' : $expose) : 'default';
114+
$expose = is_string($expose) ? ('true' === $expose ? 'default' : $expose) : 'default';
114115

115116
if (in_array($expose, $domain, true)) {
116117
$targetRoutes->add($name, $route);
117118
}
118-
119119
}
120120

121121
return $targetRoutes;

Controller/Controller.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the FOSJsRoutingBundle package.
57
*
@@ -33,13 +35,12 @@ class Controller
3335
* Default constructor.
3436
*
3537
* @param object $serializer Any object with a serialize($data, $format) method
36-
* @param ExposedRoutesExtractorInterface $exposedRoutesExtractor The extractor service.
37-
* @param array $cacheControl
38-
* @param boolean $debug
38+
* @param ExposedRoutesExtractorInterface $exposedRoutesExtractor the extractor service
39+
* @param bool $debug
3940
*/
40-
public function __construct(private mixed $serializer, private ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = array(), private bool $debug = false)
41+
public function __construct(private mixed $serializer, private ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = [], private bool $debug = false)
4142
{
42-
$this->cacheControlConfig = new CacheControlConfig($cacheControl);
43+
$this->cacheControlConfig = new CacheControlConfig($cacheControl);
4344
}
4445

4546
public function indexAction(Request $request, $_format): Response
@@ -54,13 +55,13 @@ public function indexAction(Request $request, $_format): Response
5455
$cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath($request->getLocale()), $this->debug);
5556

5657
if (!$cache->isFresh() || $this->debug) {
57-
$exposedRoutes = $this->exposedRoutesExtractor->getRoutes();
58+
$exposedRoutes = $this->exposedRoutesExtractor->getRoutes();
5859
$serializedRoutes = $this->serializer->serialize($exposedRoutes, 'json');
5960
$cache->write($serializedRoutes, $this->exposedRoutesExtractor->getResources());
6061
} else {
6162
$path = method_exists($cache, 'getPath') ? $cache->getPath() : (string) $cache;
6263
$serializedRoutes = file_get_contents($path);
63-
$exposedRoutes = $this->serializer->deserialize(
64+
$exposedRoutes = $this->serializer->deserialize(
6465
$serializedRoutes,
6566
'Symfony\Component\Routing\RouteCollection',
6667
'json'
@@ -75,7 +76,7 @@ public function indexAction(Request $request, $_format): Response
7576
$this->exposedRoutesExtractor->getPort(),
7677
$this->exposedRoutesExtractor->getScheme(),
7778
$request->getLocale(),
78-
$request->query->has('domain') ? explode(',', $request->query->get('domain')) : array()
79+
$request->query->has('domain') ? explode(',', $request->query->get('domain')) : []
7980
);
8081

8182
$content = $this->serializer->serialize($routesResponse, 'json');
@@ -86,10 +87,10 @@ public function indexAction(Request $request, $_format): Response
8687
throw new HttpException(400, 'Invalid JSONP callback value');
8788
}
8889

89-
$content = '/**/' . $callback . '(' . $content . ');';
90+
$content = '/**/'.$callback.'('.$content.');';
9091
}
9192

92-
$response = new Response($content, 200, array('Content-Type' => $request->getMimeType($_format)));
93+
$response = new Response($content, 200, ['Content-Type' => $request->getMimeType($_format)]);
9394
$this->cacheControlConfig->apply($response);
9495

9596
return $response;

DependencyInjection/Configuration.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the FOSJsRoutingBundle package.
57
*
@@ -11,8 +13,8 @@
1113

1214
namespace FOS\JsRoutingBundle\DependencyInjection;
1315

14-
use Symfony\Component\Config\Definition\ConfigurationInterface;
1516
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17+
use Symfony\Component\Config\Definition\ConfigurationInterface;
1618

1719
/**
1820
* Configuration class.
@@ -37,8 +39,8 @@ public function getConfigTreeBuilder(): TreeBuilder
3739
->scalarNode('serializer')->cannotBeEmpty()->end()
3840
->arrayNode('routes_to_expose')
3941
->beforeNormalization()
40-
->ifTrue(function ($v) { return !is_array($v); })
41-
->then(function ($v) { return array($v); })
42+
->ifTrue(fn ($v) => !is_array($v))
43+
->then(fn ($v) => [$v])
4244
->end()
4345
->prototype('scalar')->end()
4446
->end()
@@ -52,8 +54,8 @@ public function getConfigTreeBuilder(): TreeBuilder
5254
->scalarNode('smaxage')->defaultNull()->end()
5355
->arrayNode('vary')
5456
->beforeNormalization()
55-
->ifTrue(function ($v) { return !is_array($v); })
56-
->then(function ($v) { return array($v); })
57+
->ifTrue(fn ($v) => !is_array($v))
58+
->then(fn ($v) => [$v])
5759
->end()
5860
->prototype('scalar')->end()
5961
->end()

DependencyInjection/FOSJsRoutingExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the FOSJsRoutingBundle package.
57
*
@@ -11,8 +13,8 @@
1113

1214
namespace FOS\JsRoutingBundle\DependencyInjection;
1315

14-
use Symfony\Component\Config\FileLocator;
1516
use Symfony\Component\Config\Definition\Processor;
17+
use Symfony\Component\Config\FileLocator;
1618
use Symfony\Component\DependencyInjection\Alias;
1719
use Symfony\Component\DependencyInjection\ContainerBuilder;
1820
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -55,7 +57,7 @@ public function load(array $configs, ContainerBuilder $container): void
5557
if (isset($config['cache_control'])) {
5658
$config['cache_control']['enabled'] = true;
5759
} else {
58-
$config['cache_control'] = array('enabled' => false);
60+
$config['cache_control'] = ['enabled' => false];
5961
}
6062

6163
$container->setParameter('fos_js_routing.cache_control', $config['cache_control']);

0 commit comments

Comments
 (0)