Skip to content

Short array syntax #10875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions best_practices/business-logic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ Then, enable the bundle in ``AppKernel.php``, but only for the ``dev`` and
{
public function registerBundles()
{
$bundles = array(
$bundles = [
// ...
);
];

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
// ...
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}
Expand Down
10 changes: 5 additions & 5 deletions best_practices/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ for the homepage of our app::
->getRepository(Post::class)
->findLatest();

return $this->render('default/index.html.twig', array(
return $this->render('default/index.html.twig', [
'posts' => $posts,
));
]);
}
}

Expand Down Expand Up @@ -155,10 +155,10 @@ For example::
{
$deleteForm = $this->createDeleteForm($post);

return $this->render('admin/post/show.html.twig', array(
return $this->render('admin/post/show.html.twig', [
'post' => $post,
'delete_form' => $deleteForm->createView(),
));
]);
}

Normally, you'd expect a ``$id`` argument to ``showAction()``. Instead, by
Expand All @@ -182,7 +182,7 @@ manually. In our application, we have this situation in ``CommentController``::
{
$post = $this->getDoctrine()
->getRepository(Post::class)
->findOneBy(array('slug' => $postSlug));
->findOneBy(['slug' => $postSlug]);

if (!$post) {
throw $this->createNotFoundException();
Expand Down
14 changes: 7 additions & 7 deletions best_practices/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ form in its own PHP class::

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
$resolver->setDefaults([
'data_class' => Post::class,
));
]);
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ scope of that form::
{
$builder
// ...
->add('save', SubmitType::class, array('label' => 'Create Post'))
->add('save', SubmitType::class, ['label' => 'Create Post'])
;
}

Expand All @@ -123,10 +123,10 @@ some developers configure form buttons in the controller::
{
$post = new Post();
$form = $this->createForm(PostType::class, $post);
$form->add('submit', SubmitType::class, array(
$form->add('submit', SubmitType::class, [
'label' => 'Create',
'attr' => array('class' => 'btn btn-default pull-right'),
));
'attr' => ['class' => 'btn btn-default pull-right'],
]);

// ...
}
Expand Down Expand Up @@ -215,7 +215,7 @@ Handling a form submit usually follows a similar template::

return $this->redirect($this->generateUrl(
'admin_post_show',
array('id' => $post->getId())
['id' => $post->getId()]
));
}

Expand Down
4 changes: 2 additions & 2 deletions best_practices/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ the same ``getAuthorEmail()`` logic you used above::

protected function supports($attribute, $subject)
{
if (!in_array($attribute, array(self::CREATE, self::EDIT))) {
if (!in_array($attribute, [self::CREATE, self::EDIT])) {
return false;
}

Expand All @@ -309,7 +309,7 @@ the same ``getAuthorEmail()`` logic you used above::
switch ($attribute) {
case self::CREATE:
// if the user is an admin, allow them to create new posts
if ($this->decisionManager->decide($token, array('ROLE_ADMIN'))) {
if ($this->decisionManager->decide($token, ['ROLE_ADMIN'])) {
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions best_practices/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ class in the constructor of the Twig extension::

public function getFilters()
{
return array(
return [
new TwigFilter(
'md2html',
array($this, 'markdownToHtml'),
array('is_safe' => array('html'), 'pre_escape' => 'html')
[$this, 'markdownToHtml'],
['is_safe' => ['html'], 'pre_escape' => 'html']
),
);
}
Expand Down
14 changes: 7 additions & 7 deletions best_practices/tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ A functional test like this is simple to implement thanks to

public function urlProvider()
{
return array(
array('/'),
array('/posts'),
array('/post/fixture-post-1'),
array('/blog/category/fixture-category'),
array('/archives'),
return [
['/'],
['/posts'],
['/post/fixture-post-1'],
['/blog/category/fixture-category'],
['/archives'],
// ...
);
];
}
}

Expand Down
10 changes: 5 additions & 5 deletions bundles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ the ``registerBundles()`` method of the ``AppKernel`` class::
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
Expand All @@ -48,9 +48,9 @@ the ``registerBundles()`` method of the ``AppKernel`` class::
new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
);
];

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
Expand Down Expand Up @@ -107,12 +107,12 @@ Now that you've created the bundle, enable it via the ``AppKernel`` class::
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
$bundles = [
// ...

// register your bundle
new Acme\TestBundle\AcmeTestBundle(),
);
];
// ...

return $bundles;
Expand Down
8 changes: 4 additions & 4 deletions bundles/best_practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ following standardized instructions in your ``README.md`` file.
{
public function registerBundles()
{
$bundles = array(
$bundles = [
// ...
new <vendor>\<bundle-name>\<bundle-long-name>(),
);
];

// ...
}
Expand Down Expand Up @@ -278,11 +278,11 @@ following standardized instructions in your ``README.md`` file.
{
public function registerBundles()
{
$bundles = array(
$bundles = [
// ...

new <vendor>\<bundle-name>\<bundle-long-name>(),
);
];

// ...
}
Expand Down
42 changes: 21 additions & 21 deletions bundles/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ as integration of other related components:

.. code-block:: php

$container->loadFromExtension('framework', array(
$container->loadFromExtension('framework', [
'form' => true,
));
]);

.. sidebar:: Using Parameters to Configure your Bundle

Expand Down Expand Up @@ -91,10 +91,10 @@ allow users to configure it with some configuration that looks like this:
.. code-block:: php

// app/config/config.php
$container->loadFromExtension('acme_social', array(
$container->loadFromExtension('acme_social', [
'client_id' => 123,
'client_secret' => 'your_secret',
));
]);

The basic idea is that instead of having the user override individual
parameters, you let the user configure just a few, specifically created,
Expand Down Expand Up @@ -139,36 +139,36 @@ automatically converts XML and YAML to an array).
For the configuration example in the previous section, the array passed to your
``load()`` method will look like this::

array(
array(
'twitter' => array(
[
[
'twitter' => [
'client_id' => 123,
'client_secret' => 'your_secret',
),
),
)
],
],
]

Notice that this is an *array of arrays*, not just a single flat array of the
configuration values. This is intentional, as it allows Symfony to parse
several configuration resources. For example, if ``acme_social`` appears in
another configuration file - say ``config_dev.yml`` - with different values
beneath it, the incoming array might look like this::

array(
[
// values from config.yml
array(
'twitter' => array(
[
'twitter' => [
'client_id' => 123,
'client_secret' => 'your_secret',
),
),
],
],
// values from config_dev.yml
array(
'twitter' => array(
[
'twitter' => [
'client_id' => 456,
),
),
)
],
],
]

The order of the two arrays depends on which one is set first.

Expand Down Expand Up @@ -315,7 +315,7 @@ In your extension, you can load this and dynamically set its arguments::

public function load(array $configs, ContainerBuilder $container)
{
$config = array();
$config = [];
// let resources override the previous set value
foreach ($configs as $subConfig) {
$config = array_merge($config, $subConfig);
Expand Down
16 changes: 8 additions & 8 deletions bundles/extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ class names::
// ...

// this method can't compile classes that contain PHP annotations
$this->addClassesToCompile(array(
$this->addClassesToCompile([
UserManager::class,
Slugger::class,
// ...
));
]);

// add here only classes that contain PHP annotations
$this->addAnnotatedClassesToCompile(array(
$this->addAnnotatedClassesToCompile([
'AppBundle\\Controller\\DefaultController',
// ...
));
]);
}

.. note::
Expand All @@ -186,15 +186,15 @@ The classes to compile can also be added using file path patterns::
{
// ...

$this->addClassesToCompile(array(
$this->addClassesToCompile([
'**Bundle\\Manager\\',
// ...
));
]);

$this->addAnnotatedClassesToCompile(array(
$this->addAnnotatedClassesToCompile([
'**Bundle\\Controller\\',
// ...
));
]);
}

Patterns are transformed into the actual class namespaces using the classmap
Expand Down
10 changes: 5 additions & 5 deletions bundles/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ The only thing you need to do now is register the bundle in ``AppKernel``::

public function registerBundles()
{
$bundles = array(
$bundles = [
// ...
new FOS\UserBundle\FOSUserBundle(),
);
];

// ...
}
Expand All @@ -85,11 +85,11 @@ and ``test`` environments, register the bundle in this way::

public function registerBundles()
{
$bundles = array(
$bundles = [
// ...
);
];

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}

Expand Down
Loading