Skip to content

Commit b649bf4

Browse files
committed
[FEATURE] Initial commit
0 parents  commit b649bf4

File tree

11 files changed

+475
-0
lines changed

11 files changed

+475
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.build/
2+
/vendor/
3+
/composer.lock

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019 Benni Mack
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# TYPO3 Coding Standards Package
2+
3+
You know the feeling: You work on your own extension, and then contribute to TYPO3, and TYPO3 delivers all the nice
4+
things to check for proper coding standards.
5+
6+
Well, same happens to all of us! Luckily, TYPO3 has this configuration in Core, and it's now available separately
7+
- ready to plug-and-play for you!
8+
9+
It does not matter if you're an extension developer, or a TYPO3 contributor, or working on your TYPO3 project.
10+
11+
## Installation
12+
13+
As this is a composer package, execute `composer req --dev typo3/coding-standards` in your composer project.
14+
15+
## What's in the package?
16+
17+
The coding guidelines that are used in TYPO3 Core development. Instead of putting this information in our main
18+
repository, it should be helpful to apply certain rules to other projects as well. TYPO3 is more than just TYPO3 Core!
19+
20+
1. PHP-CS-Fixer rules
21+
22+
Making sure your PHP files apply to the same rules.
23+
24+
2. .editorconfig
25+
26+
If you work on a team, and you use different IDE settings, `.editorconfig` helps you to have the same settings
27+
across all editors. It does not matter if it is VS-Code, vim or PhpStorm.
28+
29+
3. eslint config
30+
31+
TypeScript rules are also available.
32+
33+
### Setting up the TYPO3 rulesets as boilerplate
34+
35+
Our coding standards file can set this up for you. Run
36+
37+
php vendor/typo3/coding-standards/setup.php project
38+
php vendor/typo3/coding-standards/setup.php extension
39+
40+
or if you want to update the rules, add `-f` option to the end.
41+
42+
Have a look at the newly created files in your root folder:
43+
44+
- .php_cs
45+
- .editorconfig
46+
47+
For projects, the folder `src/extensions` is configured by default, but you can accommodate where your extensions
48+
or PHP code resides in. For extensions, PHP-CS-Fixer scans the whole base directory.
49+
50+
In addition, you can configure your PHP-CS-Fixer cache file folder and other configurations just like with PHP-CS-Fixer.
51+
52+
53+
You can decide to commit them to your Git repository, which is the recommended way.
54+
55+
## Executing the PHP-CS-Fixer
56+
57+
Once you've followed the step above, running PHP CS Fixer works like this:
58+
59+
composer exec php-cs-fixer
60+
61+
Leave a note on how you set it up on GitHub Actions or GitLab CI/CD so this document can be even more helpful
62+
for everybody.
63+
64+
65+
## What's next?
66+
67+
We'd love to ship out license headers for all PHP files, however there are certain limitations (namespace must
68+
be underneath the license headers), which why this option is not enabled by default.
69+
70+
## A note about our standards
71+
72+
### PHP Coding Guidelines
73+
74+
TYPO3's coding guidelines have evolved over time. And we are happy to have PHP-FIG and PSR-1/PSR-2 and PSR-12.
75+
That's why we're committed to following these guidelines.
76+
77+
However, there are some more rules that we think are good:
78+
79+
- Remove leading slashes in use clauses.
80+
- PHP single-line arrays should not have trailing comma.
81+
- Single-line whitespace before closing semicolon are prohibited.
82+
- Remove unused use statements in the PHP source code
83+
- Ensure Concatenation to have at least one whitespace around
84+
- Remove trailing whitespace at the end of blank lines.
85+
86+
## Maintaining and Development of this package
87+
88+
This package is not meant to be updated regularly, since talking about coding guidelines takes a lot of time, and
89+
is usual a matter of taste. Nonetheless, you can always open up an issue if you feel like we're missing out on something.
90+
91+
## License & Thanks
92+
93+
This package is available under the MIT license, since it relies heavily on the PHP-CS-Fixer code.
94+
95+
In addition, I would like to thank the TYPO3 Core Team that kickstarted this set of rules in 2015, and to the
96+
creators and maintainers of PHP-CS-Fixer package.
97+
98+
Benni Mack
99+
TYPO3 Project Lead

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "typo3/coding-standards",
3+
"type": "coding-standards",
4+
"description": "A set of coding guidelines for any TYPO3-related project or extension",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Benni Mack",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"friendsofphp/php-cs-fixer": "^2.16.0",
14+
"php": "^7.2"
15+
},
16+
"config": {
17+
"sort-packages": true
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"TYPO3\\CodingStandards\\": "src/"
22+
}
23+
}
24+
}

setup.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env php
2+
<?php
3+
declare(strict_types = 1);
4+
5+
$setup = function($scriptPath, string $type = null, $forceOption = null) {
6+
$rootPath = getcwd();
7+
require $rootPath . '/vendor/autoload.php';
8+
$obj = new \TYPO3\CodingStandards\Setup($rootPath);
9+
switch ($type) {
10+
case 'extension':
11+
return $obj->forExtension($forceOption === '-f');
12+
case 'project':
13+
return $obj->forProject($forceOption === '-f');
14+
default:
15+
echo "You need to specify at least one option. Use 'extension' or 'project'.\n";
16+
exit(1);
17+
}
18+
};
19+
$setup(...$_SERVER['argv']);

src/CsFixerConfig.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
declare(strict_types = 1);
3+
namespace TYPO3\CodingStandards;
4+
5+
/*
6+
* This file is part of the TYPO3 project - inspiring people to share!
7+
* (c) 2019 Benni Mack
8+
*
9+
* For the full copyright and license information, please view
10+
* the LICENSE file that was distributed with this source code.
11+
*/
12+
13+
use PhpCsFixer\Config;
14+
15+
class CsFixerConfig extends Config
16+
{
17+
private static $defaultHeader = <<<EOF
18+
{header}
19+
20+
It is free software; you can redistribute it and/or modify it under
21+
the terms of the GNU General Public License, either version 2
22+
of the License, or any later version.
23+
24+
For the full copyright and license information, please read the
25+
LICENSE.txt file that was distributed with this source code.
26+
27+
The TYPO3 project - inspiring people to share!
28+
EOF;
29+
30+
private static $typo3Rules = [
31+
'@DoctrineAnnotation' => true,
32+
'@PSR2' => true,
33+
'array_syntax' => ['syntax' => 'short'],
34+
'cast_spaces' => ['space' => 'none'],
35+
'concat_space' => ['spacing' => 'one'],
36+
'declare_equal_normalize' => ['space' => 'single'],
37+
'dir_constant' => true,
38+
'function_typehint_space' => true,
39+
'hash_to_slash_comment' => true,
40+
'lowercase_cast' => true,
41+
'modernize_types_casting' => true,
42+
'native_function_casing' => true,
43+
'no_alias_functions' => true,
44+
'no_blank_lines_after_phpdoc' => true,
45+
'no_empty_phpdoc' => true,
46+
'no_empty_statement' => true,
47+
'no_extra_consecutive_blank_lines' => true,
48+
'no_leading_import_slash' => true,
49+
'no_leading_namespace_whitespace' => true,
50+
'no_null_property_initialization' => true,
51+
'no_short_bool_cast' => true,
52+
'no_singleline_whitespace_before_semicolons' => true,
53+
'no_superfluous_elseif' => true,
54+
'no_trailing_comma_in_singleline_array' => true,
55+
'no_unneeded_control_parentheses' => true,
56+
'no_unused_imports' => true,
57+
'no_useless_else' => true,
58+
'no_whitespace_in_blank_line' => true,
59+
'ordered_imports' => true,
60+
'php_unit_construct' => ['assertEquals', 'assertSame', 'assertNotEquals', 'assertNotSame'],
61+
'php_unit_mock_short_will_return' => true,
62+
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
63+
'phpdoc_no_access' => true,
64+
'phpdoc_no_empty_return' => true,
65+
'phpdoc_no_package' => true,
66+
'phpdoc_scalar' => true,
67+
'phpdoc_trim' => true,
68+
'phpdoc_types' => true,
69+
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
70+
'return_type_declaration' => ['space_before' => 'none'],
71+
'single_quote' => true,
72+
'whitespace_after_comma_in_array' => true
73+
];
74+
75+
public function __construct($name = 'TYPO3')
76+
{
77+
parent::__construct($name);
78+
}
79+
80+
public static function create()
81+
{
82+
/** @var self $obj */
83+
$obj = parent::create();
84+
$obj->setRiskyAllowed(true);
85+
// Apply our rules
86+
$obj->setRules(static::$typo3Rules);
87+
$obj->getFinder()->exclude(['vendor', 'typo3temp', 'var', '.build']);
88+
return $obj;
89+
}
90+
91+
public function addRules(array $rules)
92+
{
93+
$rules = array_replace_recursive($this->getRules(), $rules);
94+
$this->setRules($rules);
95+
}
96+
97+
public function setHeader(string $header = 'This file is part of the TYPO3 CMS project.', $replaceAll = false)
98+
{
99+
if (!$replaceAll) {
100+
$header = str_replace('{header}', $header, static::$defaultHeader);
101+
}
102+
$rules = $this->getRules();
103+
$rules['header_comment'] = [
104+
'header' => $header,
105+
'commentType' => 'comment',
106+
'location' => 'after_declare_strict',
107+
'separate' => 'both'
108+
];
109+
return parent::setRules($rules);
110+
}
111+
}

src/Setup.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
declare(strict_types = 1);
3+
namespace TYPO3\CodingStandards;
4+
5+
/*
6+
* This file is part of the TYPO3 project - inspiring people to share!
7+
* (c) 2019 Benni Mack
8+
*
9+
* For the full copyright and license information, please view
10+
* the LICENSE file that was distributed with this source code.
11+
*/
12+
13+
class Setup
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $rootPath;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $templatesPath;
24+
25+
public function __construct(string $rootPath)
26+
{
27+
$this->rootPath = $rootPath;
28+
$this->templatesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'templates';
29+
}
30+
31+
public function forProject(bool $force): int
32+
{
33+
$errors = $this->copyEditorConfig($force);
34+
// copy php-cs-fixer configuration
35+
if (file_exists($this->rootPath . '/.php_cs') && !$force) {
36+
echo "A .php_cs file already exists in your main folder, but the -f option was not set. Nothing copied.\n";
37+
$errors = true;
38+
} else {
39+
copy($this->templatesPath . '/project_php_cs.dist', $this->rootPath . '/.php_cs');
40+
}
41+
return $errors ? 1 : 0;
42+
}
43+
44+
public function forExtension(bool $force): int
45+
{
46+
$errors = $this->copyEditorConfig($force);
47+
// copy php-cs-fixer configuration
48+
if (file_exists($this->rootPath . '/.php_cs') && !$force) {
49+
echo "A .php_cs file already exists in your main folder, but the -f option was not set. Nothing copied.\n";
50+
$errors = true;
51+
} else {
52+
copy($this->templatesPath . '/extension_php_cs.dist', $this->rootPath . '/.php_cs');
53+
}
54+
return $errors ? 1 : 0;
55+
}
56+
57+
private function copyEditorConfig(bool $force): bool
58+
{
59+
$errors = false;
60+
// copy editorconfig
61+
if (file_exists($this->rootPath . '/.editorconfig') && !$force) {
62+
echo "A .editorconfig file already exists in your main folder, but the -f option was not set. Nothing copied.\n";
63+
$errors = true;
64+
} else {
65+
copy($this->templatesPath . '/editorconfig.dist', $this->rootPath . '/.editorconfig');
66+
}
67+
return $errors;
68+
}
69+
}

0 commit comments

Comments
 (0)