Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 1422aa2

Browse files
Added php-cs-fixer (#912)
Added php-cs-fixer rules
1 parent 4e9598d commit 1422aa2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1725
-3351
lines changed

.php_cs.dist

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
$header = <<<'EOF'
4+
This file is part of the SgDatatablesBundle package.
5+
6+
(c) stwe <https://github.com/stwe/DatatablesBundle>
7+
8+
For the full copyright and license information, please view the LICENSE
9+
file that was distributed with this source code.
10+
EOF;
11+
12+
$finder = PhpCsFixer\Finder::create()
13+
->exclude('vendor/')
14+
->in(__DIR__)
15+
->append([__DIR__.'/php-cs-fixer'])
16+
;
17+
18+
$config = PhpCsFixer\Config::create()
19+
->setRiskyAllowed(true)
20+
->setRules([
21+
'@PHP56Migration' => true,
22+
'@PhpCsFixer' => true,
23+
'@PhpCsFixer:risky' => true,
24+
'@PHPUnit60Migration:risky' => true,
25+
'@Symfony' => true,
26+
'array_syntax' => ['syntax' => 'short'],
27+
'header_comment' => ['header' => $header],
28+
'list_syntax' => ['syntax' => 'long'],
29+
'no_php4_constructor' => true,
30+
'no_superfluous_phpdoc_tags' => true,
31+
'no_useless_return' => true,
32+
'not_operator_with_successor_space' => true,
33+
])
34+
->setFinder($finder)
35+
;
36+
37+
return $config;

Command/CreateDatatableCommand.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
/**
3+
/*
44
* This file is part of the SgDatatablesBundle package.
55
*
66
* (c) stwe <https://github.com/stwe/DatatablesBundle>
@@ -11,23 +11,17 @@
1111

1212
namespace Sg\DatatablesBundle\Command;
1313

14+
use Doctrine\ORM\Mapping\ClassMetadataInfo;
15+
use Exception;
16+
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCommand;
17+
use Sensio\Bundle\GeneratorBundle\Command\Validators;
1418
use Sg\DatatablesBundle\Generator\DatatableGenerator;
15-
16-
use Symfony\Component\Console\Input\InputInterface;
17-
use Symfony\Component\Console\Output\OutputInterface;
1819
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
1921
use Symfony\Component\Console\Input\InputOption;
22+
use Symfony\Component\Console\Output\OutputInterface;
2023
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
21-
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCommand;
22-
use Sensio\Bundle\GeneratorBundle\Command\Validators;
23-
use Doctrine\ORM\Mapping\ClassMetadataInfo;
24-
use Exception;
2524

26-
/**
27-
* Class CreateDatatableCommand
28-
*
29-
* @package Sg\DatatablesBundle\Command
30-
*/
3125
class CreateDatatableCommand extends GenerateDoctrineCommand
3226
{
3327
//-------------------------------------------------
@@ -41,11 +35,12 @@ protected function configure()
4135
{
4236
$this
4337
->setName('sg:datatable:generate')
44-
->setAliases(array('sg:datatables:generate', 'sg:generate:datatable', 'sg:generate:datatables'))
38+
->setAliases(['sg:datatables:generate', 'sg:generate:datatable', 'sg:generate:datatables'])
4539
->setDescription('Generates a new Datatable based on a Doctrine entity.')
4640
->addArgument('entity', InputArgument::REQUIRED, 'The entity class name (shortcut notation).')
4741
->addOption('fields', 'f', InputOption::VALUE_OPTIONAL, 'The fields to create columns in the new Datatable.')
48-
->addOption('overwrite', 'o', InputOption::VALUE_NONE, 'Allow to overwrite an existing Datatable.');
42+
->addOption('overwrite', 'o', InputOption::VALUE_NONE, 'Allow to overwrite an existing Datatable.')
43+
;
4944
}
5045

5146
/**
@@ -58,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5853
list($bundle, $entity) = $this->parseShortcutNotation($entity);
5954

6055
// get entity's metadata
61-
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle)."\\".$entity;
56+
$entityClass = $this->getContainer()->get('doctrine')->getAliasNamespace($bundle).'\\'.$entity;
6257
$metadata = $this->getEntityMetadata($entityClass);
6358

6459
// get fields option
@@ -100,7 +95,7 @@ protected function createGenerator()
10095
*/
10196
protected function getSkeletonDirs(BundleInterface $bundle = null)
10297
{
103-
$skeletonDirs = array();
98+
$skeletonDirs = [];
10499
$skeletonDirs[] = $this->getContainer()->get('kernel')->locateResource('@SgDatatablesBundle/Resources/views/skeleton');
105100

106101
return $skeletonDirs;
@@ -119,12 +114,12 @@ protected function getSkeletonDirs(BundleInterface $bundle = null)
119114
*/
120115
private static function parseFields($input)
121116
{
122-
$fields = array();
117+
$fields = [];
123118

124119
foreach (explode(' ', $input) as $value) {
125120
$elements = explode(':', $value);
126121

127-
$row = array();
122+
$row = [];
128123
$row['property'] = $elements[0];
129124
$row['column_type'] = 'Column::class';
130125
$row['data'] = null;
@@ -134,9 +129,11 @@ private static function parseFields($input)
134129
switch ($elements[1]) {
135130
case 'datetime':
136131
$row['column_type'] = 'DateTimeColumn::class';
132+
137133
break;
138134
case 'boolean':
139135
$row['column_type'] = 'BooleanColumn::class';
136+
140137
break;
141138
}
142139
}
@@ -150,14 +147,11 @@ private static function parseFields($input)
150147
/**
151148
* Get Id from metadata.
152149
*
153-
* @param array $metadata
154-
*
155-
* @return mixed
156150
* @throws Exception
157151
*/
158152
private function getIdentifierFromMetadata(array $metadata)
159153
{
160-
if (count($metadata[0]->identifier) > 1) {
154+
if (\count($metadata[0]->identifier) > 1) {
161155
throw new Exception('CreateDatatableCommand::getIdentifierFromMetadata(): The Datatable generator does not support entities with multiple primary keys.');
162156
}
163157

@@ -168,24 +162,24 @@ private function getIdentifierFromMetadata(array $metadata)
168162
* Returns an array of fields. Fields can be both column fields and
169163
* association fields.
170164
*
171-
* @param ClassMetadataInfo $metadata
172-
*
173165
* @return array $fields
174166
*/
175167
private function getFieldsFromMetadata(ClassMetadataInfo $metadata)
176168
{
177-
$fields = array();
169+
$fields = [];
178170

179171
foreach ($metadata->fieldMappings as $field) {
180-
$row = array();
172+
$row = [];
181173
$row['property'] = $field['fieldName'];
182174

183175
switch ($field['type']) {
184176
case 'datetime':
185177
$row['column_type'] = 'DateTimeColumn::class';
178+
186179
break;
187180
case 'boolean':
188181
$row['column_type'] = 'BooleanColumn::class';
182+
189183
break;
190184
default:
191185
$row['column_type'] = 'Column::class';
@@ -201,11 +195,11 @@ private function getFieldsFromMetadata(ClassMetadataInfo $metadata)
201195
$targetMetadata = $this->getEntityMetadata($targetClass);
202196

203197
foreach ($targetMetadata[0]->fieldMappings as $field) {
204-
$row = array();
198+
$row = [];
205199
$row['property'] = $relation['fieldName'].'.'.$field['fieldName'];
206200
$row['column_type'] = 'Column::class';
207201
$row['title'] = ucwords(str_replace('.', ' ', $row['property']));
208-
if ($relation['type'] === ClassMetadataInfo::ONE_TO_MANY || $relation['type'] === ClassMetadataInfo::MANY_TO_MANY) {
202+
if (ClassMetadataInfo::ONE_TO_MANY === $relation['type'] || ClassMetadataInfo::MANY_TO_MANY === $relation['type']) {
209203
$row['data'] = $relation['fieldName'].'[, ].'.$field['fieldName'];
210204
} else {
211205
$row['data'] = null;

Controller/DatatableController.php

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
/**
3+
/*
44
* This file is part of the SgDatatablesBundle package.
55
*
66
* (c) stwe <https://github.com/stwe/DatatablesBundle>
@@ -11,22 +11,17 @@
1111

1212
namespace Sg\DatatablesBundle\Controller;
1313

14+
use DateTime;
15+
use Doctrine\DBAL\Types\Type;
16+
use Exception;
1417
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15-
use Symfony\Component\HttpFoundation\Response;
1618
use Symfony\Component\HttpFoundation\Request;
17-
use Symfony\Component\PropertyAccess\PropertyAccessor;
18-
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19+
use Symfony\Component\HttpFoundation\Response;
1920
use Symfony\Component\PropertyAccess\PropertyAccess;
21+
use Symfony\Component\PropertyAccess\PropertyAccessor;
2022
use Symfony\Component\Routing\Annotation\Route;
21-
use Doctrine\DBAL\Types\Type;
22-
use Exception;
23-
use DateTime;
23+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2424

25-
/**
26-
* Class DatatableController
27-
*
28-
* @package Sg\DatatablesBundle\Controller
29-
*/
3025
class DatatableController extends Controller
3126
{
3227
//-------------------------------------------------
@@ -36,12 +31,11 @@ class DatatableController extends Controller
3631
/**
3732
* Edit field.
3833
*
39-
* @param Request $request
40-
*
4134
* @Route("/datatables/edit/field", methods={"POST"}, name="sg_datatables_edit")
4235
*
43-
* @return Response
4436
* @throws Exception
37+
*
38+
* @return Response
4539
*/
4640
public function editAction(Request $request)
4741
{
@@ -58,7 +52,7 @@ public function editAction(Request $request)
5852
$path = $request->request->get('path'); // for toMany - the current element
5953

6054
// check token
61-
if (!$this->isCsrfTokenValid('sg-datatables-editable', $token)) {
55+
if (! $this->isCsrfTokenValid('sg-datatables-editable', $token)) {
6256
throw new AccessDeniedException('DatatableController::editAction(): The CSRF token is invalid.');
6357
}
6458

@@ -69,7 +63,8 @@ public function editAction(Request $request)
6963
/** @noinspection PhpUndefinedMethodInspection */
7064
$accessor = PropertyAccess::createPropertyAccessorBuilder()
7165
->enableMagicCall()
72-
->getPropertyAccessor();
66+
->getPropertyAccessor()
67+
;
7368

7469
// normalize the new value
7570
$value = $this->normalizeValue($originalTypeOfField, $value);
@@ -96,53 +91,51 @@ public function editAction(Request $request)
9691
* Finds an object by its primary key / identifier.
9792
*
9893
* @param string $entityClassName
99-
* @param mixed $pk
100-
*
101-
* @return object
10294
*/
103-
private function getEntityByPk($entityClassName, $pk)
95+
private function getEntityByPk($entityClassName, $pk): object
10496
{
10597
$em = $this->getDoctrine()->getManager();
10698

10799
$entity = $em->getRepository($entityClassName)->find($pk);
108-
if (!$entity) {
100+
if (! $entity) {
109101
throw $this->createNotFoundException('DatatableController::getEntityByPk(): The entity does not exist.');
110102
}
111103

112104
return $entity;
113105
}
114106

115107
/**
116-
* Normalize value.
117-
*
118-
* @param string $originalTypeOfField
119-
* @param mixed $value
120-
*
121-
* @return bool|DateTime|float|int|null|string
122108
* @throws Exception
109+
*
110+
* @return bool|DateTime|float|int|string|null
123111
*/
124-
private function normalizeValue($originalTypeOfField, $value)
112+
private function normalizeValue(string $originalTypeOfField, $value)
125113
{
126114
switch ($originalTypeOfField) {
127115
case Type::DATETIME:
128116
$value = new DateTime($value);
117+
129118
break;
130119
case Type::BOOLEAN:
131120
$value = $this->strToBool($value);
121+
132122
break;
133123
case Type::TEXT:
134124
case Type::STRING:
135125
break;
136126
case Type::SMALLINT:
137127
case Type::INTEGER:
138128
$value = (int) $value;
129+
139130
break;
140131
case Type::BIGINT:
141132
$value = (string) $value;
133+
142134
break;
143135
case Type::FLOAT:
144136
case Type::DECIMAL:
145137
$value = (float) $value;
138+
146139
break;
147140
default:
148141
throw new Exception("DatatableController::prepareValue(): The field type {$originalTypeOfField} is not editable.");
@@ -152,27 +145,24 @@ private function normalizeValue($originalTypeOfField, $value)
152145
}
153146

154147
/**
155-
* String to boolean.
156-
*
157-
* @param string $str
158-
*
159-
* @return null|bool
160148
* @throws Exception
161149
*/
162-
private function strToBool($str)
150+
private function strToBool(string $str): ?bool
163151
{
164152
$str = strtolower($str);
165153

166154
if ('null' === $str) {
167155
return null;
168156
}
169157

170-
if ($str === 'true' || $str === '1') {
158+
if ('true' === $str || '1' === $str) {
171159
return true;
172-
} elseif ($str === 'false' || $str === '0') {
160+
}
161+
162+
if ('false' === $str || '0' === $str) {
173163
return false;
174-
} else {
175-
throw new Exception('DatatableController::strToBool(): Cannot convert string to boolean, expected string "true" or "false".');
176164
}
165+
166+
throw new Exception('DatatableController::strToBool(): Cannot convert string to boolean, expected string "true" or "false".');
177167
}
178168
}

0 commit comments

Comments
 (0)