Permalink
Browse files

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
  • Loading branch information...
2 parents 3dd9998 + 055fc88 commit 15ef2b7eb440d10792e8a6ecee53f8a9a8f0be90 @javiereguiluz javiereguiluz committed Jun 24, 2016
View
@@ -11,7 +11,7 @@ public function registerBundles()
// application, you must add it in the following array to register it
// in the application. Otherwise, the bundle won't be enabled and you
// won't be able to use it.
- $bundles = array(
+ $bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
@@ -24,13 +24,13 @@ public function registerBundles()
new CodeExplorerBundle\CodeExplorerBundle(),
new AppBundle\AppBundle(),
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
- );
+ ];
// Some bundles are only used while developing the application or during
// the unit and functional tests. Therefore, they are only registered
// when the application runs in 'dev' or 'test' environments. This allows
// to increase application performance in the production environment.
- if (in_array($this->getEnvironment(), array('dev', 'test'))) {
+ if (in_array($this->getEnvironment(), ['dev', 'test'])) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
View
@@ -17,8 +17,8 @@ set_time_limit(0);
$loader = require __DIR__.'/../app/autoload.php';
$input = new ArgvInput();
-$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
-$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
+$env = $input->getParameterOption(['--env', '-e'], getenv('SYMFONY_ENV') ?: 'dev');
+$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(['--no-debug', '']) && $env !== 'prod';
if ($debug) {
Debug::enable();
@@ -98,20 +98,20 @@ protected function interact(InputInterface $input, OutputInterface $output)
$output->writeln('-----------------------------------');
// ...but you can also pass an array of strings to the writeln() method
- $output->writeln(array(
+ $output->writeln([
'',
'If you prefer to not use this interactive wizard, provide the',
'arguments required by this command as follows:',
'',
' $ php bin/console app:add-user username password email@example.com',
'',
- ));
+ ]);
- $output->writeln(array(
+ $output->writeln([
'',
'Now we\'ll ask you for the value of all the missing command arguments.',
'',
- ));
+ ]);
// See http://symfony.com/doc/current/components/console/helpers/questionhelper.html
$console = $this->getHelper('question');
@@ -139,7 +139,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
$password = $input->getArgument('password');
if (null === $password) {
$question = new Question(' > <info>Password</info> (your type will be hidden): ');
- $question->setValidator(array($this, 'passwordValidator'));
+ $question->setValidator([$this, 'passwordValidator']);
$question->setHidden(true);
$question->setMaxAttempts(self::MAX_ATTEMPTS);
@@ -153,7 +153,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
$email = $input->getArgument('email');
if (null === $email) {
$question = new Question(' > <info>Email</info>: ');
- $question->setValidator(array($this, 'emailValidator'));
+ $question->setValidator([$this, 'emailValidator']);
$question->setMaxAttempts(self::MAX_ATTEMPTS);
$email = $console->ask($input, $output, $question);
@@ -177,7 +177,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$isAdmin = $input->getOption('is-admin');
// first check if a user with the same username already exists
- $existingUser = $this->entityManager->getRepository('AppBundle:User')->findOneBy(array('username' => $username));
+ $existingUser = $this->entityManager->getRepository(User::class)->findOneBy(['username' => $username]);
if (null !== $existingUser) {
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)
$user = new User();
$user->setUsername($username);
$user->setEmail($email);
- $user->setRoles(array($isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER'));
+ $user->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']);
// See http://symfony.com/doc/current/book/security.html#security-encoding-password
$encoder = $this->getContainer()->get('security.password_encoder');
@@ -78,25 +78,25 @@ protected function interact(InputInterface $input, OutputInterface $output)
$output->writeln('Delete User Command Interactive Wizard');
$output->writeln('-----------------------------------');
- $output->writeln(array(
+ $output->writeln([
'',
'If you prefer to not use this interactive wizard, provide the',
'arguments required by this command as follows:',
'',
' $ php bin/console app:delete-user username',
'',
- ));
+ ]);
- $output->writeln(array(
+ $output->writeln([
'',
'Now we\'ll ask you for the value of all the missing command arguments.',
'',
- ));
+ ]);
$helper = $this->getHelper('question');
$question = new Question(' > <info>Username</info>: ');
- $question->setValidator(array($this, 'usernameValidator'));
+ $question->setValidator([$this, 'usernameValidator']);
$question->setMaxAttempts(self::MAX_ATTEMPTS);
$username = $helper->ask($input, $output, $question);
@@ -108,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$username = $input->getArgument('username');
$this->usernameValidator($username);
- $repository = $this->entityManager->getRepository('AppBundle:User');
+ $repository = $this->entityManager->getRepository(User::class);
/** @var User $user */
$user = $repository->findOneByUsername($username);
@@ -87,11 +87,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$maxResults = $input->getOption('max-results');
// Use ->findBy() instead of ->findAll() to allow result sorting and limiting
- $users = $this->entityManager->getRepository('AppBundle:User')->findBy(array(), array('id' => 'DESC'), $maxResults);
+ $users = $this->entityManager->getRepository(User::class)->findBy([], ['id' => 'DESC'], $maxResults);
// Doctrine query returns an array of objects and we need an array of plain arrays
$usersAsPlainArrays = array_map(function (User $user) {
- return array($user->getId(), $user->getUsername(), $user->getEmail(), implode(', ', $user->getRoles()));
+ return [$user->getId(), $user->getUsername(), $user->getEmail(), implode(', ', $user->getRoles())];
}, $users);
// In your console commands you should always use the regular output type,
@@ -105,7 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$table = new Table($bufferedOutput);
$table
- ->setHeaders(array('ID', 'Username', 'Email', 'Roles'))
+ ->setHeaders(['ID', 'Username', 'Email', 'Roles'])
->setRows($usersAsPlainArrays)
;
$table->render();
@@ -11,12 +11,14 @@
namespace AppBundle\Controller\Admin;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use AppBundle\Entity\Post;
+use AppBundle\Form\PostType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
-use AppBundle\Entity\Post;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\Form\Extension\Core\Type\SubmitType;
+use Symfony\Component\HttpFoundation\Request;
/**
* Controller used to manage blog contents in the backend.
@@ -52,9 +54,9 @@ class BlogController extends Controller
public function indexAction()
{
$entityManager = $this->getDoctrine()->getManager();
- $posts = $entityManager->getRepository('AppBundle:Post')->findAll();
+ $posts = $entityManager->getRepository(Post::class)->findAll();
- return $this->render('admin/blog/index.html.twig', array('posts' => $posts));
+ return $this->render('admin/blog/index.html.twig', ['posts' => $posts]);
}
/**
@@ -73,8 +75,8 @@ public function newAction(Request $request)
$post->setAuthorEmail($this->getUser()->getEmail());
// See http://symfony.com/doc/current/book/forms.html#submitting-forms-with-multiple-buttons
- $form = $this->createForm('AppBundle\Form\PostType', $post)
- ->add('saveAndCreateNew', 'Symfony\Component\Form\Extension\Core\Type\SubmitType');
+ $form = $this->createForm(PostType::class, $post)
+ ->add('saveAndCreateNew', SubmitType::class);
$form->handleRequest($request);
@@ -102,10 +104,10 @@ public function newAction(Request $request)
return $this->redirectToRoute('admin_post_index');
}
- return $this->render('admin/blog/new.html.twig', array(
+ return $this->render('admin/blog/new.html.twig', [
'post' => $post,
'form' => $form->createView(),
- ));
+ ]);
}
/**
@@ -125,10 +127,10 @@ public function showAction(Post $post)
$deleteForm = $this->createDeleteForm($post);
- return $this->render('admin/blog/show.html.twig', array(
+ return $this->render('admin/blog/show.html.twig', [
'post' => $post,
'delete_form' => $deleteForm->createView(),
- ));
+ ]);
}
/**
@@ -145,7 +147,7 @@ public function editAction(Post $post, Request $request)
$entityManager = $this->getDoctrine()->getManager();
- $editForm = $this->createForm('AppBundle\Form\PostType', $post);
+ $editForm = $this->createForm(PostType::class, $post);
$deleteForm = $this->createDeleteForm($post);
$editForm->handleRequest($request);
@@ -156,14 +158,14 @@ public function editAction(Post $post, Request $request)
$this->addFlash('success', 'post.updated_successfully');
- return $this->redirectToRoute('admin_post_edit', array('id' => $post->getId()));
+ return $this->redirectToRoute('admin_post_edit', ['id' => $post->getId()]);
}
- return $this->render('admin/blog/edit.html.twig', array(
+ return $this->render('admin/blog/edit.html.twig', [
'post' => $post,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
- ));
+ ]);
}
/**
@@ -210,7 +212,7 @@ public function deleteAction(Request $request, Post $post)
private function createDeleteForm(Post $post)
{
return $this->createFormBuilder()
- ->setAction($this->generateUrl('admin_post_delete', array('id' => $post->getId())))
+ ->setAction($this->generateUrl('admin_post_delete', ['id' => $post->getId()]))
->setMethod('DELETE')
->getForm()
;
@@ -21,6 +21,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
+use AppBundle\Form\CommentType;
/**
* Controller used to manage blog contents in the public part of the site.
@@ -40,9 +41,9 @@ class BlogController extends Controller
*/
public function indexAction($page)
{
- $posts = $this->getDoctrine()->getRepository('AppBundle:Post')->findLatest($page);
+ $posts = $this->getDoctrine()->getRepository(Post::class)->findLatest($page);
- return $this->render('blog/index.html.twig', array('posts' => $posts));
+ return $this->render('blog/index.html.twig', ['posts' => $posts]);
}
/**
@@ -66,7 +67,7 @@ public function postShowAction(Post $post)
dump($post, $this->get('security.token_storage')->getToken()->getUser(), new \DateTime());
}
- return $this->render('blog/post_show.html.twig', array('post' => $post));
+ return $this->render('blog/post_show.html.twig', ['post' => $post]);
}
/**
@@ -81,7 +82,7 @@ public function postShowAction(Post $post)
*/
public function commentNewAction(Request $request, Post $post)
{
- $form = $this->createForm('AppBundle\Form\CommentType');
+ $form = $this->createForm(CommentType::class);
$form->handleRequest($request);
@@ -95,13 +96,13 @@ public function commentNewAction(Request $request, Post $post)
$entityManager->persist($comment);
$entityManager->flush();
- return $this->redirectToRoute('blog_post', array('slug' => $post->getSlug()));
+ return $this->redirectToRoute('blog_post', ['slug' => $post->getSlug()]);
}
- return $this->render('blog/comment_form_error.html.twig', array(
+ return $this->render('blog/comment_form_error.html.twig', [
'post' => $post,
'form' => $form->createView(),
- ));
+ ]);
}
/**
@@ -118,11 +119,11 @@ public function commentNewAction(Request $request, Post $post)
*/
public function commentFormAction(Post $post)
{
- $form = $this->createForm('AppBundle\Form\CommentType');
+ $form = $this->createForm(CommentType::class);
- return $this->render('blog/_comment_form.html.twig', array(
+ return $this->render('blog/_comment_form.html.twig', [
'post' => $post,
'form' => $form->createView(),
- ));
+ ]);
}
}
@@ -31,12 +31,12 @@ public function loginAction()
{
$helper = $this->get('security.authentication_utils');
- return $this->render('security/login.html.twig', array(
+ return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => $helper->getLastUsername(),
// last authentication error (if any)
'error' => $helper->getLastAuthenticationError(),
- ));
+ ]);
}
/**
@@ -58,7 +58,7 @@ private function loadUsers(ObjectManager $manager)
$annaAdmin = new User();
$annaAdmin->setUsername('anna_admin');
$annaAdmin->setEmail('anna_admin@symfony.com');
- $annaAdmin->setRoles(array('ROLE_ADMIN'));
+ $annaAdmin->setRoles(['ROLE_ADMIN']);
$encodedPassword = $passwordEncoder->encodePassword($annaAdmin, 'kitten');
$annaAdmin->setPassword($encodedPassword);
$manager->persist($annaAdmin);
@@ -146,7 +146,7 @@ private function getPostContent()
private function getPhrases()
{
- return array(
+ return [
'Lorem ipsum dolor sit amet consectetur adipiscing elit',
'Pellentesque vitae velit ex',
'Mauris dapibus risus quis suscipit vulputate',
@@ -162,7 +162,7 @@ private function getPhrases()
'Sed varius a risus eget aliquam',
'Nunc viverra elit ac laoreet suscipit',
'Pellentesque et sapien pulvinar consectetur',
- );
+ ];
}
private function getRandomPostTitle()
@@ -45,7 +45,7 @@ class User implements UserInterface
/**
* @ORM\Column(type="json_array")
*/
- private $roles = array();
+ private $roles = [];
public function getId()
{
Oops, something went wrong.

0 comments on commit 15ef2b7

Please sign in to comment.