Skip to content

Commit

Permalink
feature #350 Use new php features (voronkovich)
Browse files Browse the repository at this point in the history
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
javiereguiluz committed Jun 24, 2016
2 parents 3dd9998 + 055fc88 commit 15ef2b7
Show file tree
Hide file tree
Showing 22 changed files with 130 additions and 124 deletions.
6 changes: 3 additions & 3 deletions app/AppKernel.php
Expand Up @@ -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(),
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions bin/console
Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions src/AppBundle/Command/AddUserCommand.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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');
Expand Down
12 changes: 6 additions & 6 deletions src/AppBundle/Command/DeleteUserCommand.php
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/AppBundle/Command/ListUsersCommand.php
Expand Up @@ -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,
Expand All @@ -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();
Expand Down
34 changes: 18 additions & 16 deletions src/AppBundle/Controller/Admin/BlogController.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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]);
}

/**
Expand All @@ -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);

Expand Down Expand Up @@ -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(),
));
]);
}

/**
Expand All @@ -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(),
));
]);
}

/**
Expand All @@ -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);
Expand All @@ -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(),
));
]);
}

/**
Expand Down Expand Up @@ -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()
;
Expand Down
21 changes: 11 additions & 10 deletions src/AppBundle/Controller/BlogController.php
Expand Up @@ -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.
Expand All @@ -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]);
}

/**
Expand All @@ -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]);
}

/**
Expand All @@ -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);

Expand All @@ -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(),
));
]);
}

/**
Expand All @@ -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(),
));
]);
}
}
4 changes: 2 additions & 2 deletions src/AppBundle/Controller/SecurityController.php
Expand Up @@ -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(),
));
]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/AppBundle/DataFixtures/ORM/LoadFixtures.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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',
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/User.php
Expand Up @@ -45,7 +45,7 @@ class User implements UserInterface
/**
* @ORM\Column(type="json_array")
*/
private $roles = array();
private $roles = [];

public function getId()
{
Expand Down

0 comments on commit 15ef2b7

Please sign in to comment.