Skip to content

Commit 47133cf

Browse files
committed
Initial commit with LaravelPhpdocAlignmentFixer
0 parents  commit 47133cf

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

.gitignore

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

LICENSE

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

README.MD

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# PHP Code Standards
2+
3+
A collection of fixers/sniffs to support PHP coding standards that haven't made their way into packages yet, bundled as a Composer package.
4+
5+
If/when they are made into official releases from the original authors, they should be deprecated here with an appropriate link to update to the official release.
6+
7+
Namespaces adhere as closely to the origin/author's naming conventions as possible.
8+
9+
## Requirements
10+
11+
* PHP >= 8.1
12+
* [Composer](http://getcomposer.org/)
13+
14+
## Installation
15+
16+
This is released as a Composer package, to install run the following command:
17+
```
18+
composer require codedruids/php-coding-standards
19+
```
20+
21+
## Current packages
22+
23+
* `LaravelPhpdocAlignmentFixer`
24+
<br>PHPDoc space alignment fixer for conforming with [Laravel](https://laravel.com) coding standards.
25+
<br>Origin: [laravel/pint](https://github.com/laravel/pint) (@nunomaduro)
26+
27+
## License
28+
29+
Unless otherwise noted by individual fixers/sniffs, this package is licensed under the MIT license - see the [LICENSE](LICENSE) file for details.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "codedruids/php-coding-standards",
3+
"description": "Collection of coding standards that haven't made it into packages yet",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Leith Caldwell",
9+
"email": "leith@codedruids.com"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"require": {
14+
"php": "^8.1.0",
15+
"friendsofphp/php-cs-fixer": "^3.16"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Laravel\\": "src/Laravel"
20+
}
21+
}
22+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Laravel\CodingStandards\Fixers;
4+
5+
use PhpCsFixer\DocBlock\TypeExpression;
6+
use PhpCsFixer\Fixer\FixerInterface;
7+
use PhpCsFixer\FixerDefinition\FixerDefinition;
8+
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
9+
use PhpCsFixer\Tokenizer\Token;
10+
use PhpCsFixer\Tokenizer\Tokens;
11+
use SplFileInfo;
12+
13+
/**
14+
* Fixer copied from https://github.com/laravel/pint
15+
* Copyright (c) 2022-2023 Nuno Maduro
16+
*
17+
* Some code in this file is part of PHP CS Fixer.
18+
*
19+
* Copyright (c) 2012-2022 Fabien Potencier, Dariusz Rumiński
20+
*
21+
* Permission is hereby granted, free of charge, to any person obtaining a copy
22+
* of this software and associated documentation files (the "Software"), to deal
23+
* in the Software without restriction, including without limitation the rights
24+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
* copies of the Software, and to permit persons to whom the Software is furnished
26+
*
27+
* The above copyright notice and this permission notice shall be included in all
28+
* copies or substantial portions of the Software.
29+
*
30+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36+
* THE SOFTWARE.
37+
*/
38+
39+
class LaravelPhpdocAlignmentFixer implements FixerInterface
40+
{
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getName(): string
45+
{
46+
return 'Laravel/laravel_phpdoc_alignment';
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function isCandidate(Tokens $tokens): bool
53+
{
54+
return $tokens->isAnyTokenKindsFound([T_DOC_COMMENT]);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function isRisky(): bool
61+
{
62+
return false;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function fix(SplFileInfo $file, Tokens $tokens): void
69+
{
70+
for ($index = $tokens->count() - 1; $index > 0; $index--) {
71+
if (! $tokens[$index]->isGivenKind([\T_DOC_COMMENT])) {
72+
continue;
73+
}
74+
75+
$newContent = preg_replace_callback(
76+
'/(?P<tag>@param)\s+(?P<hint>(?:'.TypeExpression::REGEX_TYPES.')?)\s+(?P<var>(?:&|\.{3})?\$\S+)/ux',
77+
fn ($matches) => $matches['tag'].' '.$matches['hint'].' '.$matches['var'],
78+
$tokens[$index]->getContent()
79+
);
80+
81+
if ($newContent == $tokens[$index]->getContent()) {
82+
continue;
83+
}
84+
85+
$tokens[$index] = new Token([T_DOC_COMMENT, $newContent]);
86+
}
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function getDefinition(): FixerDefinitionInterface
93+
{
94+
return new FixerDefinition('@param and type definition must be followed by two spaces.', []);
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function getPriority(): int
101+
{
102+
return -42;
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function supports(SplFileInfo $file): bool
109+
{
110+
return true;
111+
}
112+
}

0 commit comments

Comments
 (0)