Browse Source

Replace more arrays in favor of the shorter syntax.

Ber Clausen 12 years ago
parent
commit
334c56f19c

+ 17 - 17
src/Routing/Route/Route.php

@@ -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.
@@ -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
@@ -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;
@@ -168,11 +168,11 @@ class Route {
 	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);
@@ -295,7 +295,7 @@ class Route {
 		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) {
@@ -373,7 +373,7 @@ class Route {
  * @return array Array of passed args.
  */
 	protected function _parseArgs($args, $context) {
-		$pass = array();
+		$pass = [];
 		$args = explode('/', $args);
 
 		foreach ($args as $param) {
@@ -398,7 +398,7 @@ class Route {
  *   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();
 		}
@@ -434,7 +434,7 @@ class Route {
 		}
 
 		// Missing defaults is a fail.
-		if (array_diff_key($defaults, $url) !== array()) {
+		if (array_diff_key($defaults, $url) !== []) {
 			return false;
 		}
 
@@ -449,8 +449,8 @@ class Route {
 			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.
@@ -506,12 +506,12 @@ class Route {
  * @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;

+ 13 - 13
src/Routing/RouteCollection.php

@@ -30,7 +30,7 @@ class RouteCollection implements \Countable {
  *
  * @var array
  */
-	protected $_routeTable = array();
+	protected $_routeTable = [];
 
 /**
  * A list of routes connected, in the order they were connected.
@@ -38,7 +38,7 @@ class RouteCollection implements \Countable {
  *
  * @var array
  */
-	protected $_routes = array();
+	protected $_routes = [];
 
 /**
  * The top most request's context. Updated whenever
@@ -46,12 +46,12 @@ class RouteCollection implements \Countable {
  *
  * @var array
  */
-	protected $_requestContext = array(
+	protected $_requestContext = [
 		'_base' => '',
 		'_port' => 80,
 		'_scheme' => 'http',
 		'_host' => 'localhost',
-	);
+	];
 
 /**
  * Add a route to the collection.
@@ -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;
@@ -123,22 +123,22 @@ class RouteCollection implements \Countable {
 		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']);
@@ -219,7 +219,7 @@ class RouteCollection implements \Countable {
  */
 	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;
@@ -242,12 +242,12 @@ class RouteCollection implements \Countable {
  * @return void
  */
 	public function setContext(Request $request) {
-		$this->_requestContext = array(
+		$this->_requestContext = [
 			'_base' => $request->base,
 			'_port' => $request->port(),
 			'_scheme' => $request->scheme(),
 			'_host' => $request->host()
-		);
+		];
 	}
 
 /**

+ 12 - 12
src/Routing/Router.php

@@ -328,10 +328,10 @@ class Router {
  * @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');
 		}
@@ -382,7 +382,7 @@ class Router {
  * @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);
@@ -434,7 +434,7 @@ class Router {
  * @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
@@ -541,7 +541,7 @@ class Router {
 			static::pushRequest($request);
 		} else {
 			$requestData = $request;
-			$requestData += array(array(), array());
+			$requestData += array([], []);
 			$requestData[0] += array(
 				'controller' => false,
 				'action' => false,
@@ -713,14 +713,14 @@ class Router {
  * @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;
@@ -888,14 +888,14 @@ class Router {
  * @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'],
@@ -1016,13 +1016,13 @@ class Router {
  * @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;

+ 8 - 8
tests/TestCase/Routing/DispatcherTest.php

@@ -119,7 +119,7 @@ class MyPluginController extends MyPluginAppController {
  *
  * @var array
  */
-	public $uses = array();
+	public $uses = [];
 
 /**
  * index method
@@ -169,7 +169,7 @@ class OtherPagesController extends MyPluginAppController {
  *
  * @var array
  */
-	public $uses = array();
+	public $uses = [];
 
 /**
  * display method
@@ -217,7 +217,7 @@ class ArticlesTestController extends ArticlesTestAppController {
  *
  * @var array
  */
-	public $uses = array();
+	public $uses = [];
 
 /**
  * admin_index method
@@ -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);
@@ -273,7 +273,7 @@ class DispatcherTest extends TestCase {
 	public function tearDown() {
 		parent::tearDown();
 		Plugin::unload();
-		Configure::write('Dispatcher.filters', array());
+		Configure::write('Dispatcher.filters', []);
 	}
 
 /**
@@ -932,7 +932,7 @@ class DispatcherTest extends TestCase {
 		$event = new Event(__CLASS__, $dispatcher, array('request' => $request));
 		$dispatcher->parseParams($event);
 		$expected = array(
-			'pass' => array(),
+			'pass' => [],
 			'plugin' => null,
 			'controller' => 'posts',
 			'action' => 'add',
@@ -1012,7 +1012,7 @@ class DispatcherTest extends TestCase {
 		$event = new Event(__CLASS__, $dispatcher, array('request' => $request));
 		$dispatcher->parseParams($event);
 		$expected = array(
-			'pass' => array(),
+			'pass' => [],
 			'plugin' => null,
 			'controller' => 'posts',
 			'action' => 'add',
@@ -1053,7 +1053,7 @@ class DispatcherTest extends TestCase {
 		foreach ($_SERVER as $key => $val) {
 			unset($_SERVER[$key]);
 		}
-		Configure::write('App', array());
+		Configure::write('App', []);
 	}
 
 /**

+ 8 - 8
tests/TestCase/Routing/RouterTest.php

@@ -416,36 +416,36 @@ class RouterTest extends TestCase {
 	public function testGenerateUrlResourceRoute() {
 		Router::mapResources('Posts');
 
-		$result = Router::url(array(
+		$result = Router::url([
 			'controller' => 'posts',
 			'action' => 'index',
 			'[method]' => 'GET'
-		));
+		]);
 		$expected = '/posts';
 		$this->assertEquals($expected, $result);
 
-		$result = Router::url(array(
+		$result = Router::url([
 			'controller' => 'posts',
 			'action' => 'view',
 			'[method]' => 'GET',
 			'id' => 10
-		));
+		]);
 		$expected = '/posts/10';
 		$this->assertEquals($expected, $result);
 
-		$result = Router::url(array('controller' => 'posts', 'action' => 'add', '[method]' => 'POST'));
+		$result = Router::url(['controller' => 'posts', 'action' => 'add', '[method]' => 'POST']);
 		$expected = '/posts';
 		$this->assertEquals($expected, $result);
 
-		$result = Router::url(array('controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'id' => 10));
+		$result = Router::url(['controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'id' => 10]);
 		$expected = '/posts/10';
 		$this->assertEquals($expected, $result);
 
-		$result = Router::url(array('controller' => 'posts', 'action' => 'delete', '[method]' => 'DELETE', 'id' => 10));
+		$result = Router::url(['controller' => 'posts', 'action' => 'delete', '[method]' => 'DELETE', 'id' => 10]);
 		$expected = '/posts/10';
 		$this->assertEquals($expected, $result);
 
-		$result = Router::url(array('controller' => 'posts', 'action' => 'edit', '[method]' => 'POST', 'id' => 10));
+		$result = Router::url(['controller' => 'posts', 'action' => 'edit', '[method]' => 'POST', 'id' => 10]);
 		$expected = '/posts/10';
 		$this->assertEquals($expected, $result);
 	}