Skip to content
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

Adding PSR12 coding standard #56

Merged
merged 12 commits into from
Oct 25, 2019
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
50 changes: 33 additions & 17 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php


/**
* This file represents the configuration for Code Sniffing PSR-2-related
* automatic checks of coding guidelines
Expand All @@ -23,39 +24,54 @@ if (PHP_SAPI !== 'cli') {
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@DoctrineAnnotation' => true,
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'cast_spaces' => ['space' => 'none'],
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'dir_constant' => true,
'function_typehint_space' => true,
'general_phpdoc_annotation_remove' => [
'author'
],
'hash_to_slash_comment' => true,
'lowercase_cast' => true,
'modernize_types_casting' => true,
'native_function_casing' => true,
'no_alias_functions' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'no_leading_import_slash' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_leading_namespace_whitespace' => true,
'no_null_property_initialization' => true,
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_superfluous_elseif' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_unneeded_control_parentheses' => true,
'no_unused_imports' => true,
'concat_space' => ['spacing' => 'one'],
'no_useless_else' => true,
'no_whitespace_in_blank_line' => true,
'ordered_imports' => true,
'single_quote' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'phpdoc_no_access' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_package' => true,
'phpdoc_scalar' => true,
'no_blank_lines_after_phpdoc' => true,
'array_syntax' => ['syntax' => 'short'],
'whitespace_after_comma_in_array' => true,
'function_typehint_space' => true,
'hash_to_slash_comment' => true,
'no_alias_functions' => true,
'lowercase_cast' => true,
'no_leading_namespace_whitespace' => true,
'native_function_casing' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'return_type_declaration' => ['space_before' => 'none'],
'self_accessor' => true,
'no_short_bool_cast' => true,
'no_unneeded_control_parentheses' => true
'single_quote' => true,
'whitespace_after_comma_in_array' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('.build')
->exclude('var')
->exclude('public')
->in(__DIR__)
);
);
9 changes: 5 additions & 4 deletions Classes/ContentObject/JsonContentObject.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\ContentObject;

/***
*
Expand All @@ -14,6 +11,10 @@
*
***/

declare(strict_types=1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this change intended (moving declare and namespace after comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it's intended:
https://www.php-fig.org/psr/psr-12/#3-declare-statements-namespace-and-import-statements
The order now is first open tag, then file-comment, then declare, then import, then rest

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, looks good then

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'no how that rule got accepted to the standard. Can only assume the reviewers missed it. I've never seen any code before that followed that rule of having the doc above the declares. The 2 authors of the PSR must have wanted it, only...


namespace FriendsOfTYPO3\Headless\ContentObject;

use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -166,7 +167,7 @@ protected function recursiveFind(array $haystack, $needle)
* @param array $data
* @return array
*/
protected function decodeFieldsIfRequired(array $data) : array
protected function decodeFieldsIfRequired(array $data): array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I've added a rule to check this one in #58

{
$json = [];

Expand Down
7 changes: 4 additions & 3 deletions Classes/DataProcessing/FilesProcessor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\DataProcessing;

/***
*
Expand All @@ -14,6 +11,10 @@
*
***/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\DataProcessing;

use FriendsOfTYPO3\Headless\Utility\FileUtility;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down
1 change: 1 addition & 0 deletions Classes/DataProcessing/GalleryProcessor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\DataProcessing;
Expand Down
1 change: 1 addition & 0 deletions Classes/DataProcessing/MenuProcessor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\DataProcessing;
Expand Down
3 changes: 2 additions & 1 deletion Classes/Hooks/TypolinkHook.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Hooks;

Expand Down
12 changes: 6 additions & 6 deletions Classes/Resource/Rendering/VimeoRenderer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Resource\Rendering;

/***
*
Expand All @@ -14,6 +11,10 @@
*
***/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Resource\Rendering;

use TYPO3\CMS\Core\Resource\FileInterface;

/**
Expand All @@ -39,13 +40,12 @@ public function getPriority(): int
* @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
* @return string
*/
public function render(FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
public function render(FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false): string
{
if ($options['returnUrl'] === true) {
$src = $this->createVimeoUrl($options, $file);
return htmlspecialchars($src, ENT_QUOTES | ENT_HTML5);
} else {
return parent::render(...func_get_args());
}
return parent::render(...func_get_args());
}
}
10 changes: 5 additions & 5 deletions Classes/Resource/Rendering/YouTubeRenderer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Resource\Rendering;

/***
*
Expand All @@ -14,6 +11,10 @@
*
***/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Resource\Rendering;

use TYPO3\CMS\Core\Resource\FileInterface;

/**
Expand Down Expand Up @@ -44,8 +45,7 @@ public function render(FileInterface $file, $width, $height, array $options = []
if ($options['returnUrl'] === true) {
$src = $this->createYouTubeUrl($options, $file);
return htmlspecialchars($src, ENT_QUOTES | ENT_HTML5);
} else {
return parent::render(...func_get_args());
}
return parent::render(...func_get_args());
}
}
13 changes: 7 additions & 6 deletions Classes/Service/PaginationService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Service;
Expand Down Expand Up @@ -104,7 +105,7 @@ public function __construct(QueryResultInterface $objects, int $itemsPerPage = 1
public function paginate(int $currentPage = 1): array
{
// set current page
$this->currentPage = (integer)$currentPage;
$this->currentPage = (int)$currentPage;
if ($this->currentPage < 1) {
$this->currentPage = 1;
}
Expand All @@ -114,11 +115,11 @@ public function paginate(int $currentPage = 1): array
$modifiedObjects = null;
} else {
// modify query
$itemsPerPage = (integer)$this->configuration['itemsPerPage'];
$itemsPerPage = (int)$this->configuration['itemsPerPage'];
$query = $this->objects->getQuery();

if ($this->currentPage === $this->numberOfPages && $this->initialLimit > 0) {
$difference = $this->initialLimit - ((integer)($itemsPerPage * ($this->currentPage - 1)));
$difference = $this->initialLimit - ((int)($itemsPerPage * ($this->currentPage - 1)));
if ($difference > 0) {
$query->setLimit($difference);
} else {
Expand All @@ -129,7 +130,7 @@ public function paginate(int $currentPage = 1): array
}

if ($this->currentPage > 1) {
$offset = (integer)($itemsPerPage * ($this->currentPage - 1));
$offset = (int)($itemsPerPage * ($this->currentPage - 1));
$offset += $this->initialOffset;
$query->setOffset($offset);
} elseif ($this->initialOffset > 0) {
Expand Down Expand Up @@ -198,8 +199,8 @@ protected function calculateDisplayRange(): void
if ($this->displayRangeEnd > $this->numberOfPages) {
$this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
}
$this->displayRangeStart = (integer)max($this->displayRangeStart, 1);
$this->displayRangeEnd = (integer)min($this->displayRangeEnd, $this->numberOfPages);
$this->displayRangeStart = (int)max($this->displayRangeStart, 1);
$this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Classes/Utility/ContentUtility.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which rule from PSR-12 applies here? in TYPO3 Core it's formatted with spaces around "="

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My checks went entirely after PSR12, no additional special configuration

Declare statements MUST contain no spaces and MUST be exactly declare(strict_types=1) (with an optional semi-colon terminator).
https://www.php-fig.org/psr/psr-12/#3-declare-statements-namespace-and-import-statements

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right!


namespace FriendsOfTYPO3\Headless\Utility;

Expand Down
7 changes: 4 additions & 3 deletions Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
declare(strict_types = 1);

namespace FriendsOfTYPO3\Headless\Utility;

/***
*
Expand All @@ -14,6 +11,10 @@
*
***/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Utility;

use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Resource\AbstractFile;
Expand Down
3 changes: 2 additions & 1 deletion Classes/ViewHelpers/Format/Json/DecodeViewHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
namespace FriendsOfTYPO3\Headless\ViewHelpers\Format\Json;

/***
*
Expand All @@ -12,6 +11,8 @@
*
***/

namespace FriendsOfTYPO3\Headless\ViewHelpers\Format\Json;

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
Expand Down
7 changes: 4 additions & 3 deletions Classes/ViewHelpers/Iterator/ExplodeViewHelper.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
namespace FriendsOfTYPO3\Headless\ViewHelpers\Iterator;

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/***
*
Expand All @@ -14,6 +11,10 @@
*
***/

namespace FriendsOfTYPO3\Headless\ViewHelpers\Iterator;

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
* Explode ViewHelper
* Explodes a string by $glue
Expand Down
2 changes: 1 addition & 1 deletion Configuration/TypoScript/Helper/Page.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,4 @@ lib.page {
}
}
}
}
}