Skip to content

Commit 13a185f

Browse files
committed
bump php version and cleanup code
1 parent cfd06d1 commit 13a185f

14 files changed

+80
-84
lines changed

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
language: php
22

33
php:
4-
- 5.3
5-
- 5.4
64
- 5.5
75
- 5.6
86
- 7.0
7+
- 7.1
98
- hhvm
109

1110
sudo: false
@@ -20,7 +19,7 @@ matrix:
2019
include:
2120
- php: 5.6
2221
env: DEPS=dev
23-
- php: 5.3
22+
- php: 5.5
2423
env: COMPOSER_FLAGS="--prefer-lowest"
2524
- php: 5.6
2625
env: SYMFONY_VERSION=2.3.*
@@ -36,7 +35,7 @@ before_install:
3635
- composer self-update
3736
- if [ "$DEPS" = "dev" ]; then perl -pi -e 's/^}$/,"minimum-stability":"dev"}/' composer.json; fi
3837
- if [ "$SYMFONY_VERSION" != "" ]; then composer require symfony/symfony:${SYMFONY_VERSION} --no-update; fi
39-
38+
4039
install: composer update --prefer-dist $COMPOSER_FLAGS
4140

4241
script: phpunit

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"php": "^5.3.9|^7.0",
15+
"php": "^5.5.9|^7.0",
1616
"symfony/routing": "^2.2|3.*",
1717
"symfony/http-kernel": "^2.2|3.*",
1818
"psr/log": "1.*"

src/ChainRouteCollection.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ public function get($name)
105105
return $route;
106106
}
107107
}
108-
109-
return;
110108
}
111109

112110
/**
@@ -243,12 +241,11 @@ public function setMethods($methods)
243241
*/
244242
public function getResources()
245243
{
246-
$resources = array();
247-
foreach ($this->routeCollections as $routeCollection) {
248-
$resources = array_merge($resources, $routeCollection->getResources());
249-
}
244+
$resources = array_map(function (RouteCollection $routeCollection) {
245+
return $routeCollection->getResources();
246+
}, $this->routeCollections);
250247

251-
return array_unique($resources);
248+
return array_unique(call_user_func_array('array_merge', $resources));
252249
}
253250

254251
/**

src/ChainRouter.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function add($router, $priority = 0)
9898
*/
9999
public function all()
100100
{
101-
if (empty($this->sortedRouters)) {
101+
if (0 === count($this->sortedRouters)) {
102102
$this->sortedRouters = $this->sortRouters();
103103

104104
// setContext() is done here instead of in add() to avoid fatal errors when clearing and warming up caches
@@ -124,14 +124,9 @@ public function all()
124124
*/
125125
protected function sortRouters()
126126
{
127-
$sortedRouters = array();
128127
krsort($this->routers);
129128

130-
foreach ($this->routers as $routers) {
131-
$sortedRouters = array_merge($sortedRouters, $routers);
132-
}
133-
134-
return $sortedRouters;
129+
return call_user_func_array('array_merge', $this->routers);
135130
}
136131

137132
/**
@@ -179,7 +174,7 @@ private function doMatch($pathinfo, Request $request = null)
179174
// the request/url match logic is the same as in Symfony/Component/HttpKernel/EventListener/RouterListener.php
180175
// matching requests is more powerful than matching URLs only, so try that first
181176
if ($router instanceof RequestMatcherInterface) {
182-
if (empty($requestForMatching)) {
177+
if (null === $requestForMatching) {
183178
$requestForMatching = $this->rebuildRequest($pathinfo);
184179
}
185180

@@ -351,6 +346,6 @@ public function getRouteCollection()
351346
*/
352347
public function hasRouters()
353348
{
354-
return !empty($this->routers);
349+
return 0 < count($this->routers);
355350
}
356351
}

src/ContentAwareGenerator.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ContentAwareGenerator extends ProviderBasedGenerator
3333
*
3434
* @var string
3535
*/
36-
protected $defaultLocale = null;
36+
protected $defaultLocale;
3737

3838
/**
3939
* The content repository used to find content by it's id
@@ -89,7 +89,7 @@ public function generate($name, $parameters = array(), $absolute = UrlGeneratorI
8989
/**
9090
* Get the route by a string name.
9191
*
92-
* @param string $route
92+
* @param string $name
9393
* @param array $parameters
9494
*
9595
* @return SymfonyRoute
@@ -99,7 +99,7 @@ public function generate($name, $parameters = array(), $absolute = UrlGeneratorI
9999
protected function getRouteByName($name, array $parameters)
100100
{
101101
$route = $this->provider->getRouteByName($name);
102-
if (empty($route)) {
102+
if (null === $route) {
103103
throw new RouteNotFoundException('No route found for name: '.$name);
104104
}
105105

@@ -163,11 +163,11 @@ protected function getRouteByContent($name, &$parameters)
163163
{
164164
if ($name instanceof RouteReferrersReadInterface) {
165165
$content = $name;
166-
} elseif (isset($parameters['content_id'])
166+
} elseif (array_key_exists('content_id', $parameters)
167167
&& null !== $this->contentRepository
168168
) {
169169
$content = $this->contentRepository->findById($parameters['content_id']);
170-
if (empty($content)) {
170+
if (null === $content) {
171171
throw new RouteNotFoundException('The content repository found nothing at id '.$parameters['content_id']);
172172
}
173173
if (!$content instanceof RouteReferrersReadInterface) {
@@ -179,7 +179,7 @@ protected function getRouteByContent($name, &$parameters)
179179
}
180180

181181
$routes = $content->getRoutes();
182-
if (empty($routes)) {
182+
if (0 === count($routes)) {
183183
$hint = ($this->contentRepository && $this->contentRepository->getContentId($content))
184184
? $this->contentRepository->getContentId($content)
185185
: get_class($content);
@@ -202,8 +202,8 @@ protected function getRouteByContent($name, &$parameters)
202202
}
203203

204204
/**
205-
* @param RouteCollection $routes
206-
* @param string $locale
205+
* @param RouteCollection|SymfonyRoute[] $routes
206+
* @param string $locale
207207
*
208208
* @return bool|SymfonyRoute false if no route requirement matches the provided locale
209209
*/
@@ -232,7 +232,7 @@ protected function getRouteByLocale($routes, $locale)
232232
*/
233233
private function checkLocaleRequirement(SymfonyRoute $route, $locale)
234234
{
235-
return empty($locale)
235+
return !$locale
236236
|| !$route->getRequirement('_locale')
237237
|| preg_match('/'.$route->getRequirement('_locale').'/', $locale)
238238
;
@@ -249,7 +249,7 @@ private function checkLocaleRequirement(SymfonyRoute $route, $locale)
249249
*/
250250
protected function getLocale($parameters)
251251
{
252-
if (isset($parameters['_locale'])) {
252+
if (array_key_exists('_locale', $parameters)) {
253253
return $parameters['_locale'];
254254
}
255255

@@ -273,6 +273,8 @@ public function setDefaultLocale($locale)
273273

274274
/**
275275
* We additionally support empty name and data in parameters and RouteAware content.
276+
*
277+
* {@inheritdoc}
276278
*/
277279
public function supports($name)
278280
{
@@ -284,7 +286,7 @@ public function supports($name)
284286
*/
285287
public function getRouteDebugMessage($name, array $parameters = array())
286288
{
287-
if (empty($name) && isset($parameters['content_id'])) {
289+
if (!$name && array_key_exists('content_id', $parameters)) {
288290
return 'Content id '.$parameters['content_id'];
289291
}
290292

@@ -307,14 +309,13 @@ public function getRouteDebugMessage($name, array $parameters = array())
307309
protected function unsetLocaleIfNotNeeded(SymfonyRoute $route, array &$parameters)
308310
{
309311
$locale = $this->getLocale($parameters);
310-
if (null !== $locale) {
311-
if (preg_match('/'.$route->getRequirement('_locale').'/', $locale)
312-
&& $locale == $route->getDefault('_locale')
313-
) {
314-
$compiledRoute = $route->compile();
315-
if (!in_array('_locale', $compiledRoute->getVariables())) {
316-
unset($parameters['_locale']);
317-
}
312+
if (null !== $locale
313+
&& preg_match('/'.$route->getRequirement('_locale').'/', $locale)
314+
&& $locale == $route->getDefault('_locale')
315+
) {
316+
$compiledRoute = $route->compile();
317+
if (!in_array('_locale', $compiledRoute->getVariables())) {
318+
unset($parameters['_locale']);
318319
}
319320
}
320321
}

src/DependencyInjection/Compiler/RegisterRouteEnhancersPass.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,24 @@
2525
class RegisterRouteEnhancersPass implements CompilerPassInterface
2626
{
2727
/**
28-
* @var string
28+
* @var string Service name of the dynamic router
2929
*/
30-
protected $dynamicRouterService;
30+
private $dynamicRouterService;
3131

32-
protected $enhancerTag;
32+
/**
33+
* @var string Name of the tag
34+
*/
35+
private $enhancerTag;
3336

3437
public function __construct($dynamicRouterService = 'cmf_routing.dynamic_router', $enhancerTag = 'dynamic_router_route_enhancer')
3538
{
3639
$this->dynamicRouterService = $dynamicRouterService;
3740
$this->enhancerTag = $enhancerTag;
3841
}
3942

43+
/**
44+
* {@inheritdoc}
45+
*/
4046
public function process(ContainerBuilder $container)
4147
{
4248
if (!$container->hasDefinition($this->dynamicRouterService)) {

src/DynamicRouter.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DynamicRouter implements RouterInterface, RequestMatcherInterface, Chained
5454
protected $eventDispatcher;
5555

5656
/**
57-
* @var RouteEnhancerInterface[]
57+
* @var RouteEnhancerInterface[][]
5858
*/
5959
protected $enhancers = array();
6060

@@ -73,6 +73,11 @@ class DynamicRouter implements RouterInterface, RequestMatcherInterface, Chained
7373
*/
7474
protected $uriFilterRegexp;
7575

76+
/**
77+
* @var RouteProviderInterface
78+
*/
79+
private $provider;
80+
7681
/**
7782
* @var RequestContext
7883
*/
@@ -90,6 +95,8 @@ class DynamicRouter implements RouterInterface, RequestMatcherInterface, Chained
9095
* @param string $uriFilterRegexp
9196
* @param EventDispatcherInterface|null $eventDispatcher
9297
* @param RouteProviderInterface $provider
98+
*
99+
* @throws \InvalidArgumentException If the matcher is not a request or url matcher
93100
*/
94101
public function __construct(RequestContext $context,
95102
$matcher,
@@ -100,7 +107,9 @@ public function __construct(RequestContext $context,
100107
) {
101108
$this->context = $context;
102109
if (!$matcher instanceof RequestMatcherInterface && !$matcher instanceof UrlMatcherInterface) {
103-
throw new \InvalidArgumentException('Matcher must implement either Symfony\Component\Routing\Matcher\RequestMatcherInterface or Symfony\Component\Routing\Matcher\UrlMatcherInterface');
110+
throw new \InvalidArgumentException(
111+
sprintf('Matcher must implement either %s or %s', RequestMatcherInterface::class, UrlMatcherInterface::class)
112+
);
104113
}
105114
$this->matcher = $matcher;
106115
$this->generator = $generator;
@@ -259,7 +268,7 @@ public function matchRequest(Request $request)
259268
$this->eventDispatcher->dispatch(Events::PRE_DYNAMIC_MATCH_REQUEST, $event);
260269
}
261270

262-
if (!empty($this->uriFilterRegexp)
271+
if ($this->uriFilterRegexp
263272
&& !preg_match($this->uriFilterRegexp, $request->getPathInfo())
264273
) {
265274
throw new ResourceNotFoundException("{$request->getPathInfo()} does not match the '{$this->uriFilterRegexp}' pattern");
@@ -301,6 +310,8 @@ protected function applyRouteEnhancers($defaults, Request $request)
301310
*
302311
* @param RouteEnhancerInterface $enhancer
303312
* @param int $priority
313+
*
314+
* @return self
304315
*/
305316
public function addRouteEnhancer(RouteEnhancerInterface $enhancer, $priority = 0)
306317
{
@@ -321,7 +332,7 @@ public function addRouteEnhancer(RouteEnhancerInterface $enhancer, $priority = 0
321332
*/
322333
public function getRouteEnhancers()
323334
{
324-
if (empty($this->sortedEnhancers)) {
335+
if (0 === count($this->sortedEnhancers)) {
325336
$this->sortedEnhancers = $this->sortRouteEnhancers();
326337
}
327338

@@ -337,14 +348,9 @@ public function getRouteEnhancers()
337348
*/
338349
protected function sortRouteEnhancers()
339350
{
340-
$sortedEnhancers = array();
341351
krsort($this->enhancers);
342352

343-
foreach ($this->enhancers as $enhancers) {
344-
$sortedEnhancers = array_merge($sortedEnhancers, $enhancers);
345-
}
346-
347-
return $sortedEnhancers;
353+
return call_user_func_array('array_merge', $this->enhancers);
348354
}
349355

350356
/**

src/Enhancer/ContentRepositoryEnhancer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ public function __construct(
6161
*/
6262
public function enhance(array $defaults, Request $request)
6363
{
64-
if (!isset($defaults[$this->target]) && isset($defaults[$this->source])) {
65-
$defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
64+
if (array_key_exists($this->target, $defaults)
65+
|| !array_key_exists($this->source, $defaults)
66+
) {
67+
return $defaults;
6668
}
6769

70+
$defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
71+
6872
return $defaults;
6973
}
7074
}

src/Enhancer/FieldByClassEnhancer.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,9 @@ public function __construct($source, $target, $map)
5959
*/
6060
public function enhance(array $defaults, Request $request)
6161
{
62-
if (isset($defaults[$this->target])) {
63-
// no need to do anything
64-
return $defaults;
65-
}
66-
67-
if (!isset($defaults[$this->source])) {
62+
if (array_key_exists($this->target, $defaults)
63+
|| !array_key_exists($this->source, $defaults)
64+
) {
6865
return $defaults;
6966
}
7067

src/Enhancer/FieldMapEnhancer.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,10 @@ public function __construct($source, $target, array $hashmap)
5353
*/
5454
public function enhance(array $defaults, Request $request)
5555
{
56-
if (isset($defaults[$this->target])) {
57-
return $defaults;
58-
}
59-
if (!isset($defaults[$this->source])) {
60-
return $defaults;
61-
}
62-
if (!isset($this->hashmap[$defaults[$this->source]])) {
56+
if (array_key_exists($this->target, $defaults)
57+
|| !array_key_exists($this->source, $defaults)
58+
|| !array_key_exists($defaults[$this->source], $this->hashmap)
59+
) {
6360
return $defaults;
6461
}
6562

0 commit comments

Comments
 (0)