Skip to content

Commit

Permalink
Replace more arrays in favor of the shorter syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
bar committed Mar 31, 2014
1 parent 60150a5 commit 334c56f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 58 deletions.
34 changes: 17 additions & 17 deletions src/Routing/Route/Route.php
Expand Up @@ -33,21 +33,21 @@ class Route {
*
* @var array
*/
public $keys = array();
public $keys = [];

/**
* An array of additional parameters for the Route.
*
* @var array
*/
public $options = array();
public $options = [];

/**
* Default parameters for a Route
*
* @var array
*/
public $defaults = array();
public $defaults = [];

/**
* The routes template string.
Expand Down Expand Up @@ -83,18 +83,18 @@ class Route {
*
* @var array
*/
protected $_headerMap = array(
protected $_headerMap = [
'type' => 'content_type',
'method' => 'request_method',
'server' => 'server_name'
);
];

/**
* List of connected extensions for this route.
*
* @var array
*/
protected $_extensions = array();
protected $_extensions = [];

/**
* Constructor for a Route
Expand All @@ -110,7 +110,7 @@ class Route {
* @param array $defaults Array of defaults for the route.
* @param array $options Array of additional options for the Route
*/
public function __construct($template, $defaults = array(), $options = array()) {
public function __construct($template, $defaults = [], $options = []) {
$this->template = $template;
$this->defaults = (array)$defaults;
$this->options = (array)$options;
Expand Down Expand Up @@ -168,11 +168,11 @@ public function compile() {
protected function _writeRoute() {
if (empty($this->template) || ($this->template === '/')) {
$this->_compiledRoute = '#^/*$#';
$this->keys = array();
$this->keys = [];
return;
}
$route = $this->template;
$names = $routeParams = array();
$names = $routeParams = [];
$parsed = preg_quote($this->template, '#');

preg_match_all('#:([A-Za-z0-9_-]+[A-Z0-9a-z])#', $route, $namedElements);
Expand Down Expand Up @@ -295,7 +295,7 @@ public function parse($url) {
for ($i = 0; $i <= $count; $i++) {
unset($route[$i]);
}
$route['pass'] = array();
$route['pass'] = [];

// Assign defaults, set passed args to pass
foreach ($this->defaults as $key => $value) {
Expand Down Expand Up @@ -373,7 +373,7 @@ protected function _parseExtension($url) {
* @return array Array of passed args.
*/
protected function _parseArgs($args, $context) {
$pass = array();
$pass = [];
$args = explode('/', $args);

foreach ($args as $param) {
Expand All @@ -398,7 +398,7 @@ protected function _parseArgs($args, $context) {
* directory.
* @return mixed Either a string url for the parameters if they match or false.
*/
public function match($url, $context = array()) {
public function match($url, $context = []) {
if (!$this->compiled()) {
$this->compile();
}
Expand Down Expand Up @@ -434,7 +434,7 @@ public function match($url, $context = array()) {
}

// Missing defaults is a fail.
if (array_diff_key($defaults, $url) !== array()) {
if (array_diff_key($defaults, $url) !== []) {
return false;
}

Expand All @@ -449,8 +449,8 @@ public function match($url, $context = array()) {
return false;
}

$pass = array();
$query = array();
$pass = [];
$query = [];

foreach ($url as $key => $value) {
// keys that exist in the defaults and have different values is a match failure.
Expand Down Expand Up @@ -506,12 +506,12 @@ public function match($url, $context = array()) {
* @param array $query An array of parameters
* @return string Composed route string.
*/
protected function _writeUrl($params, $pass = array(), $query = array()) {
protected function _writeUrl($params, $pass = [], $query = []) {
$pass = implode('/', array_map('rawurlencode', $pass));
$out = $this->template;

if (!empty($this->keys)) {
$search = $replace = array();
$search = $replace = [];

foreach ($this->keys as $key) {
$string = null;
Expand Down
26 changes: 13 additions & 13 deletions src/Routing/RouteCollection.php
Expand Up @@ -30,28 +30,28 @@ class RouteCollection implements \Countable {
*
* @var array
*/
protected $_routeTable = array();
protected $_routeTable = [];

/**
* A list of routes connected, in the order they were connected.
* Used for parsing incoming urls.
*
* @var array
*/
protected $_routes = array();
protected $_routes = [];

/**
* The top most request's context. Updated whenever
* requests are pushed/popped off the stack in Router.
*
* @var array
*/
protected $_requestContext = array(
protected $_requestContext = [
'_base' => '',
'_port' => 80,
'_scheme' => 'http',
'_host' => 'localhost',
);
];

/**
* Add a route to the collection.
Expand All @@ -63,7 +63,7 @@ class RouteCollection implements \Countable {
public function add(Route $route) {
$name = $route->getName();
if (!isset($this->_routeTable[$name])) {
$this->_routeTable[$name] = array();
$this->_routeTable[$name] = [];
}
$this->_routeTable[$name][] = $route;
$this->_routes[] = $route;
Expand Down Expand Up @@ -123,22 +123,22 @@ protected function _getNames($url) {
if (isset($url['plugin'])) {
$plugin = $url['plugin'];
}
$fallbacks = array(
$fallbacks = [
'%2$s:%3$s',
'%2$s:_action',
'_controller:%3$s',
'_controller:_action'
);
];
if ($plugin) {
$fallbacks = array(
$fallbacks = [
'%1$s.%2$s:%3$s',
'%1$s.%2$s:_action',
'%1$s._controller:%3$s',
'%1$s._controller:_action',
'_plugin._controller:%3$s',
'_plugin._controller:_action',
'_controller:_action',
);
'_controller:_action'
];
}
foreach ($fallbacks as $i => $template) {
$fallbacks[$i] = sprintf($template, $plugin, $url['controller'], $url['action']);
Expand Down Expand Up @@ -219,7 +219,7 @@ public function promote($which) {
*/
public function get($index) {
if (is_string($index)) {
$routes = isset($this->_routeTable[$index]) ? $this->_routeTable[$index] : array(null);
$routes = isset($this->_routeTable[$index]) ? $this->_routeTable[$index] : [null];
return $routes[0];
}
return isset($this->_routes[$index]) ? $this->_routes[$index] : null;
Expand All @@ -242,12 +242,12 @@ public function count() {
* @return void
*/
public function setContext(Request $request) {
$this->_requestContext = array(
$this->_requestContext = [
'_base' => $request->base,
'_port' => $request->port(),
'_scheme' => $request->scheme(),
'_host' => $request->host()
);
];
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/Routing/Router.php
Expand Up @@ -328,10 +328,10 @@ public static function resourceMap($resourceMap = null) {
* @return void
* @throws \Cake\Error\Exception
*/
public static function connect($route, $defaults = array(), $options = array()) {
public static function connect($route, $defaults = [], $options = []) {
static::$initialized = true;

$defaults += array('plugin' => null);
$defaults += ['plugin' => null];
if (empty($options['action'])) {
$defaults += array('action' => 'index');
}
Expand Down Expand Up @@ -382,7 +382,7 @@ public static function connect($route, $defaults = array(), $options = array())
* @see routes
* @return array Array of routes
*/
public static function redirect($route, $url, $options = array()) {
public static function redirect($route, $url, $options = []) {
$options['routeClass'] = 'Cake\Routing\Route\RedirectRoute';
if (is_string($url)) {
$url = array('redirect' => $url);
Expand Down Expand Up @@ -434,7 +434,7 @@ public static function redirect($route, $url, $options = array()) {
* @param array $options Options to use when generating REST routes
* @return array Array of mapped resources
*/
public static function mapResources($controller, $options = array()) {
public static function mapResources($controller, $options = []) {
$options = array_merge(array(
'connectOptions' => [],
'id' => static::ID . '|' . static::UUID
Expand Down Expand Up @@ -541,7 +541,7 @@ public static function setRequestInfo($request) {
static::pushRequest($request);
} else {
$requestData = $request;
$requestData += array(array(), array());
$requestData += array([], []);
$requestData[0] += array(
'controller' => false,
'action' => false,
Expand Down Expand Up @@ -713,14 +713,14 @@ protected static function _applyUrlFilters($url) {
* @return string Full translated URL with base path.
* @throws \Cake\Error\Exception When the route name is not found
*/
public static function url($url = null, $options = array()) {
public static function url($url = null, $options = []) {
if (!static::$initialized) {
static::_loadRoutes();
}

$full = false;
if (is_bool($options)) {
list($full, $options) = array($options, array());
list($full, $options) = array($options, []);
}
$urlType = gettype($url);
$hasLeadingSlash = $plainString = false;
Expand Down Expand Up @@ -888,14 +888,14 @@ public static function fullBaseUrl($base = null) {
* @return string The string that is the reversed result of the array
*/
public static function reverse($params, $full = false) {
$url = array();
$url = [];
if ($params instanceof Request) {
$url = $params->query;
$params = $params->params;
} elseif (isset($params['url'])) {
$url = $params['url'];
}
$pass = isset($params['pass']) ? $params['pass'] : array();
$pass = isset($params['pass']) ? $params['pass'] : [];

unset(
$params['pass'], $params['paging'], $params['models'], $params['url'], $url['url'],
Expand Down Expand Up @@ -1016,13 +1016,13 @@ public static function extensions() {
* @param array $options The array of options.
* @return \Cake\Network\Request The modified request
*/
public static function parseNamedParams(Request $request, $options = array()) {
public static function parseNamedParams(Request $request, $options = []) {
$options += array('separator' => ':');
if (empty($request->params['pass'])) {
$request->params['named'] = array();
$request->params['named'] = [];
return $request;
}
$named = array();
$named = [];
foreach ($request->params['pass'] as $key => $value) {
if (strpos($value, $options['separator']) === false) {
continue;
Expand Down
16 changes: 8 additions & 8 deletions tests/TestCase/Routing/DispatcherTest.php
Expand Up @@ -119,7 +119,7 @@ class MyPluginController extends MyPluginAppController {
*
* @var array
*/
public $uses = array();
public $uses = [];

/**
* index method
Expand Down Expand Up @@ -169,7 +169,7 @@ class OtherPagesController extends MyPluginAppController {
*
* @var array
*/
public $uses = array();
public $uses = [];

/**
* display method
Expand Down Expand Up @@ -217,7 +217,7 @@ class ArticlesTestController extends ArticlesTestAppController {
*
* @var array
*/
public $uses = array();
public $uses = [];

/**
* admin_index method
Expand Down Expand Up @@ -252,7 +252,7 @@ class DispatcherTest extends TestCase {
*/
public function setUp() {
parent::setUp();
$_GET = array();
$_GET = [];

Configure::write('App.base', false);
Configure::write('App.baseUrl', false);
Expand All @@ -273,7 +273,7 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
Plugin::unload();
Configure::write('Dispatcher.filters', array());
Configure::write('Dispatcher.filters', []);
}

/**
Expand Down Expand Up @@ -932,7 +932,7 @@ public function testHttpMethodOverrides() {
$event = new Event(__CLASS__, $dispatcher, array('request' => $request));
$dispatcher->parseParams($event);
$expected = array(
'pass' => array(),
'pass' => [],
'plugin' => null,
'controller' => 'posts',
'action' => 'add',
Expand Down Expand Up @@ -1012,7 +1012,7 @@ public function testHttpMethodOverrides() {
$event = new Event(__CLASS__, $dispatcher, array('request' => $request));
$dispatcher->parseParams($event);
$expected = array(
'pass' => array(),
'pass' => [],
'plugin' => null,
'controller' => 'posts',
'action' => 'add',
Expand Down Expand Up @@ -1053,7 +1053,7 @@ protected function _reloadEnvironment() {
foreach ($_SERVER as $key => $val) {
unset($_SERVER[$key]);
}
Configure::write('App', array());
Configure::write('App', []);
}

/**
Expand Down

0 comments on commit 334c56f

Please sign in to comment.