Skip to content

Commit 15ef2b7

Browse files
committed
feature #350 Use new php features (voronkovich)
This PR was squashed before being merged into the master branch (closes #350). Discussion ---------- Use new php features See #346 Commits ------- 055fc88 Use new php features
2 parents 3dd9998 + 055fc88 commit 15ef2b7

22 files changed

+130
-124
lines changed

app/AppKernel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function registerBundles()
1111
// application, you must add it in the following array to register it
1212
// in the application. Otherwise, the bundle won't be enabled and you
1313
// won't be able to use it.
14-
$bundles = array(
14+
$bundles = [
1515
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
1616
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
1717
new Symfony\Bundle\TwigBundle\TwigBundle(),
@@ -24,13 +24,13 @@ public function registerBundles()
2424
new CodeExplorerBundle\CodeExplorerBundle(),
2525
new AppBundle\AppBundle(),
2626
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
27-
);
27+
];
2828

2929
// Some bundles are only used while developing the application or during
3030
// the unit and functional tests. Therefore, they are only registered
3131
// when the application runs in 'dev' or 'test' environments. This allows
3232
// to increase application performance in the production environment.
33-
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
33+
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
3434
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
3535
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
3636
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();

bin/console

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ set_time_limit(0);
1717
$loader = require __DIR__.'/../app/autoload.php';
1818

1919
$input = new ArgvInput();
20-
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
21-
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
20+
$env = $input->getParameterOption(['--env', '-e'], getenv('SYMFONY_ENV') ?: 'dev');
21+
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(['--no-debug', '']) && $env !== 'prod';
2222

2323
if ($debug) {
2424
Debug::enable();

src/AppBundle/Command/AddUserCommand.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,20 @@ protected function interact(InputInterface $input, OutputInterface $output)
9898
$output->writeln('-----------------------------------');
9999

100100
// ...but you can also pass an array of strings to the writeln() method
101-
$output->writeln(array(
101+
$output->writeln([
102102
'',
103103
'If you prefer to not use this interactive wizard, provide the',
104104
'arguments required by this command as follows:',
105105
'',
106106
' $ php bin/console app:add-user username password [email protected]',
107107
'',
108-
));
108+
]);
109109

110-
$output->writeln(array(
110+
$output->writeln([
111111
'',
112112
'Now we\'ll ask you for the value of all the missing command arguments.',
113113
'',
114-
));
114+
]);
115115

116116
// See http://symfony.com/doc/current/components/console/helpers/questionhelper.html
117117
$console = $this->getHelper('question');
@@ -139,7 +139,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
139139
$password = $input->getArgument('password');
140140
if (null === $password) {
141141
$question = new Question(' > <info>Password</info> (your type will be hidden): ');
142-
$question->setValidator(array($this, 'passwordValidator'));
142+
$question->setValidator([$this, 'passwordValidator']);
143143
$question->setHidden(true);
144144
$question->setMaxAttempts(self::MAX_ATTEMPTS);
145145

@@ -153,7 +153,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
153153
$email = $input->getArgument('email');
154154
if (null === $email) {
155155
$question = new Question(' > <info>Email</info>: ');
156-
$question->setValidator(array($this, 'emailValidator'));
156+
$question->setValidator([$this, 'emailValidator']);
157157
$question->setMaxAttempts(self::MAX_ATTEMPTS);
158158

159159
$email = $console->ask($input, $output, $question);
@@ -177,7 +177,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
177177
$isAdmin = $input->getOption('is-admin');
178178

179179
// first check if a user with the same username already exists
180-
$existingUser = $this->entityManager->getRepository('AppBundle:User')->findOneBy(array('username' => $username));
180+
$existingUser = $this->entityManager->getRepository(User::class)->findOneBy(['username' => $username]);
181181

182182
if (null !== $existingUser) {
183183
throw new \RuntimeException(sprintf('There is already a user registered with the "%s" username.', $username));
@@ -187,7 +187,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
187187
$user = new User();
188188
$user->setUsername($username);
189189
$user->setEmail($email);
190-
$user->setRoles(array($isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER'));
190+
$user->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']);
191191

192192
// See http://symfony.com/doc/current/book/security.html#security-encoding-password
193193
$encoder = $this->getContainer()->get('security.password_encoder');

src/AppBundle/Command/DeleteUserCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,25 @@ protected function interact(InputInterface $input, OutputInterface $output)
7878
$output->writeln('Delete User Command Interactive Wizard');
7979
$output->writeln('-----------------------------------');
8080

81-
$output->writeln(array(
81+
$output->writeln([
8282
'',
8383
'If you prefer to not use this interactive wizard, provide the',
8484
'arguments required by this command as follows:',
8585
'',
8686
' $ php bin/console app:delete-user username',
8787
'',
88-
));
88+
]);
8989

90-
$output->writeln(array(
90+
$output->writeln([
9191
'',
9292
'Now we\'ll ask you for the value of all the missing command arguments.',
9393
'',
94-
));
94+
]);
9595

9696
$helper = $this->getHelper('question');
9797

9898
$question = new Question(' > <info>Username</info>: ');
99-
$question->setValidator(array($this, 'usernameValidator'));
99+
$question->setValidator([$this, 'usernameValidator']);
100100
$question->setMaxAttempts(self::MAX_ATTEMPTS);
101101

102102
$username = $helper->ask($input, $output, $question);
@@ -108,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
108108
$username = $input->getArgument('username');
109109
$this->usernameValidator($username);
110110

111-
$repository = $this->entityManager->getRepository('AppBundle:User');
111+
$repository = $this->entityManager->getRepository(User::class);
112112
/** @var User $user */
113113
$user = $repository->findOneByUsername($username);
114114

src/AppBundle/Command/ListUsersCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
8787
{
8888
$maxResults = $input->getOption('max-results');
8989
// Use ->findBy() instead of ->findAll() to allow result sorting and limiting
90-
$users = $this->entityManager->getRepository('AppBundle:User')->findBy(array(), array('id' => 'DESC'), $maxResults);
90+
$users = $this->entityManager->getRepository(User::class)->findBy([], ['id' => 'DESC'], $maxResults);
9191

9292
// Doctrine query returns an array of objects and we need an array of plain arrays
9393
$usersAsPlainArrays = array_map(function (User $user) {
94-
return array($user->getId(), $user->getUsername(), $user->getEmail(), implode(', ', $user->getRoles()));
94+
return [$user->getId(), $user->getUsername(), $user->getEmail(), implode(', ', $user->getRoles())];
9595
}, $users);
9696

9797
// In your console commands you should always use the regular output type,
@@ -105,7 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
105105

106106
$table = new Table($bufferedOutput);
107107
$table
108-
->setHeaders(array('ID', 'Username', 'Email', 'Roles'))
108+
->setHeaders(['ID', 'Username', 'Email', 'Roles'])
109109
->setRows($usersAsPlainArrays)
110110
;
111111
$table->render();

src/AppBundle/Controller/Admin/BlogController.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace AppBundle\Controller\Admin;
1313

14-
use Symfony\Component\HttpFoundation\Request;
15-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14+
use AppBundle\Entity\Post;
15+
use AppBundle\Form\PostType;
1616
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
1717
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
1818
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
19-
use AppBundle\Entity\Post;
19+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
20+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
21+
use Symfony\Component\HttpFoundation\Request;
2022

2123
/**
2224
* Controller used to manage blog contents in the backend.
@@ -52,9 +54,9 @@ class BlogController extends Controller
5254
public function indexAction()
5355
{
5456
$entityManager = $this->getDoctrine()->getManager();
55-
$posts = $entityManager->getRepository('AppBundle:Post')->findAll();
57+
$posts = $entityManager->getRepository(Post::class)->findAll();
5658

57-
return $this->render('admin/blog/index.html.twig', array('posts' => $posts));
59+
return $this->render('admin/blog/index.html.twig', ['posts' => $posts]);
5860
}
5961

6062
/**
@@ -73,8 +75,8 @@ public function newAction(Request $request)
7375
$post->setAuthorEmail($this->getUser()->getEmail());
7476

7577
// See http://symfony.com/doc/current/book/forms.html#submitting-forms-with-multiple-buttons
76-
$form = $this->createForm('AppBundle\Form\PostType', $post)
77-
->add('saveAndCreateNew', 'Symfony\Component\Form\Extension\Core\Type\SubmitType');
78+
$form = $this->createForm(PostType::class, $post)
79+
->add('saveAndCreateNew', SubmitType::class);
7880

7981
$form->handleRequest($request);
8082

@@ -102,10 +104,10 @@ public function newAction(Request $request)
102104
return $this->redirectToRoute('admin_post_index');
103105
}
104106

105-
return $this->render('admin/blog/new.html.twig', array(
107+
return $this->render('admin/blog/new.html.twig', [
106108
'post' => $post,
107109
'form' => $form->createView(),
108-
));
110+
]);
109111
}
110112

111113
/**
@@ -125,10 +127,10 @@ public function showAction(Post $post)
125127

126128
$deleteForm = $this->createDeleteForm($post);
127129

128-
return $this->render('admin/blog/show.html.twig', array(
130+
return $this->render('admin/blog/show.html.twig', [
129131
'post' => $post,
130132
'delete_form' => $deleteForm->createView(),
131-
));
133+
]);
132134
}
133135

134136
/**
@@ -145,7 +147,7 @@ public function editAction(Post $post, Request $request)
145147

146148
$entityManager = $this->getDoctrine()->getManager();
147149

148-
$editForm = $this->createForm('AppBundle\Form\PostType', $post);
150+
$editForm = $this->createForm(PostType::class, $post);
149151
$deleteForm = $this->createDeleteForm($post);
150152

151153
$editForm->handleRequest($request);
@@ -156,14 +158,14 @@ public function editAction(Post $post, Request $request)
156158

157159
$this->addFlash('success', 'post.updated_successfully');
158160

159-
return $this->redirectToRoute('admin_post_edit', array('id' => $post->getId()));
161+
return $this->redirectToRoute('admin_post_edit', ['id' => $post->getId()]);
160162
}
161163

162-
return $this->render('admin/blog/edit.html.twig', array(
164+
return $this->render('admin/blog/edit.html.twig', [
163165
'post' => $post,
164166
'edit_form' => $editForm->createView(),
165167
'delete_form' => $deleteForm->createView(),
166-
));
168+
]);
167169
}
168170

169171
/**
@@ -210,7 +212,7 @@ public function deleteAction(Request $request, Post $post)
210212
private function createDeleteForm(Post $post)
211213
{
212214
return $this->createFormBuilder()
213-
->setAction($this->generateUrl('admin_post_delete', array('id' => $post->getId())))
215+
->setAction($this->generateUrl('admin_post_delete', ['id' => $post->getId()]))
214216
->setMethod('DELETE')
215217
->getForm()
216218
;

src/AppBundle/Controller/BlogController.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
2222
use Symfony\Component\HttpFoundation\Request;
2323
use Symfony\Component\HttpFoundation\Response;
24+
use AppBundle\Form\CommentType;
2425

2526
/**
2627
* Controller used to manage blog contents in the public part of the site.
@@ -40,9 +41,9 @@ class BlogController extends Controller
4041
*/
4142
public function indexAction($page)
4243
{
43-
$posts = $this->getDoctrine()->getRepository('AppBundle:Post')->findLatest($page);
44+
$posts = $this->getDoctrine()->getRepository(Post::class)->findLatest($page);
4445

45-
return $this->render('blog/index.html.twig', array('posts' => $posts));
46+
return $this->render('blog/index.html.twig', ['posts' => $posts]);
4647
}
4748

4849
/**
@@ -66,7 +67,7 @@ public function postShowAction(Post $post)
6667
dump($post, $this->get('security.token_storage')->getToken()->getUser(), new \DateTime());
6768
}
6869

69-
return $this->render('blog/post_show.html.twig', array('post' => $post));
70+
return $this->render('blog/post_show.html.twig', ['post' => $post]);
7071
}
7172

7273
/**
@@ -81,7 +82,7 @@ public function postShowAction(Post $post)
8182
*/
8283
public function commentNewAction(Request $request, Post $post)
8384
{
84-
$form = $this->createForm('AppBundle\Form\CommentType');
85+
$form = $this->createForm(CommentType::class);
8586

8687
$form->handleRequest($request);
8788

@@ -95,13 +96,13 @@ public function commentNewAction(Request $request, Post $post)
9596
$entityManager->persist($comment);
9697
$entityManager->flush();
9798

98-
return $this->redirectToRoute('blog_post', array('slug' => $post->getSlug()));
99+
return $this->redirectToRoute('blog_post', ['slug' => $post->getSlug()]);
99100
}
100101

101-
return $this->render('blog/comment_form_error.html.twig', array(
102+
return $this->render('blog/comment_form_error.html.twig', [
102103
'post' => $post,
103104
'form' => $form->createView(),
104-
));
105+
]);
105106
}
106107

107108
/**
@@ -118,11 +119,11 @@ public function commentNewAction(Request $request, Post $post)
118119
*/
119120
public function commentFormAction(Post $post)
120121
{
121-
$form = $this->createForm('AppBundle\Form\CommentType');
122+
$form = $this->createForm(CommentType::class);
122123

123-
return $this->render('blog/_comment_form.html.twig', array(
124+
return $this->render('blog/_comment_form.html.twig', [
124125
'post' => $post,
125126
'form' => $form->createView(),
126-
));
127+
]);
127128
}
128129
}

src/AppBundle/Controller/SecurityController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public function loginAction()
3131
{
3232
$helper = $this->get('security.authentication_utils');
3333

34-
return $this->render('security/login.html.twig', array(
34+
return $this->render('security/login.html.twig', [
3535
// last username entered by the user (if any)
3636
'last_username' => $helper->getLastUsername(),
3737
// last authentication error (if any)
3838
'error' => $helper->getLastAuthenticationError(),
39-
));
39+
]);
4040
}
4141

4242
/**

src/AppBundle/DataFixtures/ORM/LoadFixtures.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function loadUsers(ObjectManager $manager)
5858
$annaAdmin = new User();
5959
$annaAdmin->setUsername('anna_admin');
6060
$annaAdmin->setEmail('[email protected]');
61-
$annaAdmin->setRoles(array('ROLE_ADMIN'));
61+
$annaAdmin->setRoles(['ROLE_ADMIN']);
6262
$encodedPassword = $passwordEncoder->encodePassword($annaAdmin, 'kitten');
6363
$annaAdmin->setPassword($encodedPassword);
6464
$manager->persist($annaAdmin);
@@ -146,7 +146,7 @@ private function getPostContent()
146146

147147
private function getPhrases()
148148
{
149-
return array(
149+
return [
150150
'Lorem ipsum dolor sit amet consectetur adipiscing elit',
151151
'Pellentesque vitae velit ex',
152152
'Mauris dapibus risus quis suscipit vulputate',
@@ -162,7 +162,7 @@ private function getPhrases()
162162
'Sed varius a risus eget aliquam',
163163
'Nunc viverra elit ac laoreet suscipit',
164164
'Pellentesque et sapien pulvinar consectetur',
165-
);
165+
];
166166
}
167167

168168
private function getRandomPostTitle()

src/AppBundle/Entity/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class User implements UserInterface
4545
/**
4646
* @ORM\Column(type="json_array")
4747
*/
48-
private $roles = array();
48+
private $roles = [];
4949

5050
public function getId()
5151
{

0 commit comments

Comments
 (0)