Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- There is always Unreleased section on the top. Subsections (Added, Changed, Fixed, Removed) should be added as needed. -->

## Unreleased
- Remove skip of BlankLineAfterOpeningTagFixer to be PSR-12 compliant.

## 4.2.0 - 2025-02-28
- Rename the package from "Alma Career Czechia Standard for PHP" to more broad "Alma Career Coding Standard for PHP". 🎉
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ composer require --dev almacareer/coding-standard
1. Create `ecs.php` file in the root directory of your project and import the code-style rules:

```php
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use Lmc\CodingStandard\Set\SetList;
use Symplify\EasyCodingStandard\Config\ECSConfig;
Expand Down Expand Up @@ -92,7 +94,9 @@ If needed, you can also override any default settings.
Below find examples of some more opinionated checks you may want to add depending on your needs:

```php
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use Lmc\CodingStandard\Set\SetList;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff;
Expand Down Expand Up @@ -133,7 +137,9 @@ See [EasyCodingStandard docs][ecs-docs] for more configuration options.
You can configure your `ecs.php` file to entirely skip some files, disable specific checks, or suppress specific errors.

```php
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays\ArrayDeclarationSniff;
Expand Down
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@
],
"require": {
"php": "^8.0",
"friendsofphp/php-cs-fixer": "^3.66.0",
"friendsofphp/php-cs-fixer": "^3.66",
"slevomat/coding-standard": "^8.6",
"squizlabs/php_codesniffer": "^3.9",
"symplify/easy-coding-standard": "^12.2.0 <12.4 || ^12.5.5"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.42.0",
"nikic/php-parser": "<5.0",
"php-parallel-lint/php-parallel-lint": "^1.4.0",
"ergebnis/composer-normalize": "^2.42",
"nikic/php-parser": "^4.19",
"php-parallel-lint/php-parallel-lint": "^1.4",
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan": "^1.11.1",
"phpstan/phpstan-phpunit": "^1.4.0",
"phpstan/phpstan-phpunit": "^1.4",
"phpunit/phpunit": "^9.6.19"
},
"prefer-stable": true,
"autoload": {
"psr-4": {
"Lmc\\CodingStandard\\": "src/"
}
},
"files": [
"src/polyfill.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
6 changes: 4 additions & 2 deletions ecs-internal.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use Lmc\CodingStandard\Set\SetList;
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
Expand All @@ -18,7 +20,7 @@
SetList::ALMACAREER,
],
)
->withConfiguredRule(PhpUnitTestAnnotationFixer::class, ['style' => 'annotation'])
->withConfiguredRule(PhpUnitTestAnnotationFixer::class, ['style' => 'prefix'])
->withConfiguredRule(
LineLengthFixer::class,
['line_length' => 120, 'break_long_lines' => true, 'inline_short_lines' => false],
Expand Down
8 changes: 3 additions & 5 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use Lmc\CodingStandard\Fixer\SpecifyArgSeparatorFixer;
use Lmc\CodingStandard\Sniffs\Naming\AbstractClassNameSniff;
Expand Down Expand Up @@ -127,7 +129,6 @@
use PhpCsFixer\Fixer\Phpdoc\PhpdocTrimFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocVarAnnotationCorrectOrderFixer;
use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;
use PhpCsFixer\Fixer\PhpTag\FullOpeningTagFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitConstructFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer;
Expand Down Expand Up @@ -638,7 +639,4 @@
// Skip unwanted rules from ReturnTypeHintSniff
ReturnTypeHintSniff::class . '.' . ReturnTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION => null,
ReturnTypeHintSniff::class . '.' . ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT => null,

// We use declare(strict_types=1); after opening tag
BlankLineAfterOpeningTagFixer::class => null,
]);
7 changes: 0 additions & 7 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ parameters:
bootstrapFiles:
- vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/autoload.php
- vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/src/Util/Tokens.php
ignoreErrors:
- path: %currentWorkingDirectory%/tests/Fixer/SpecifyArgSeparatorFixerTest.php
message: '#Parameter \#1 \$code of static method PhpCsFixer\\Tokenizer\\Tokens::fromCode\(\) expects string, string\|false given#'
- path: %currentWorkingDirectory%/tests/Integration/CodingStandardTest.php
message: '#Parameter \#2 \$actualString of method PHPUnit\\Framework\\Assert::assertStringEqualsFile\(\) expects string, string\|false given#'
- identifier: missingType.iterableValue
path: %currentWorkingDirectory%/tests/*
36 changes: 15 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/tests/bootstrap.php"
colors="true"
>
<testsuites>
<testsuite name="unit">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>

<php>
<!-- E_ALL = 30719 -->
<ini name="error_reporting" value="30719"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="./vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/tests/bootstrap.php" colors="true">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="unit">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<php>
<!-- E_ALL = 30719 -->
<ini name="error_reporting" value="30719"/>
</php>
</phpunit>
4 changes: 3 additions & 1 deletion src/Fixer/SpecifyArgSeparatorFixer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lmc\CodingStandard\Fixer;

Expand Down
6 changes: 4 additions & 2 deletions src/Helper/Naming.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/*
* Originally part of https://github.com/symplify/symplify
Expand Down Expand Up @@ -79,7 +81,7 @@ public function getClassName(File $file, int $classNameStartPosition): string

$fqnClassName = $this->getFqnClassName($file, $completeClassName, $classNameStartPosition);
if ($fqnClassName !== '') {
return ltrim($fqnClassName, self::NAMESPACE_SEPARATOR);
return mb_ltrim($fqnClassName, self::NAMESPACE_SEPARATOR);
}

return $completeClassName;
Expand Down
4 changes: 3 additions & 1 deletion src/Helper/SniffClassWrapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/*
* Originally part of https://github.com/symplify/symplify
Expand Down
4 changes: 3 additions & 1 deletion src/Set/SetList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lmc\CodingStandard\Set;

Expand Down
4 changes: 3 additions & 1 deletion src/Sniffs/Naming/AbstractClassNameSniff.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/*
* Originally part of https://github.com/symplify/symplify
Expand Down
4 changes: 3 additions & 1 deletion src/Sniffs/Naming/ClassNameSuffixByParentSniff.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/*
* Originally part of https://github.com/symplify/symplify
Expand Down
4 changes: 3 additions & 1 deletion src/Sniffs/Naming/InterfaceNameSniff.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/*
* Originally part of https://github.com/symplify/symplify
Expand Down
4 changes: 3 additions & 1 deletion src/Sniffs/Naming/TraitNameSniff.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/*
* Originally part of https://github.com/symplify/symplify
Expand Down
35 changes: 35 additions & 0 deletions src/polyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* Polyfill for mb_ltrim() which is only available in PHP 8.4+
*
* This file provides backward compatibility for the mb_ltrim() function
* introduced in PHP 8.4.0, allowing the codebase to work with PHP 8.0-8.3.
*/

if (!function_exists('mb_ltrim')) {
/**
* Strip whitespace (or other characters) from the beginning of a string using multibyte encoding.
*
* @param string $string The input string
* @param non-empty-string $characters The characters to strip
* @param string|null $encoding The character encoding. If null, the internal character encoding is used.
*
* @return string The trimmed string
*/
function mb_ltrim(string $string, string $characters = " \n\r\t\v\0", ?string $encoding = null): string
{
$encoding = $encoding ?? mb_internal_encoding();

$characterArray = preg_split('//u', $characters, -1, PREG_SPLIT_NO_EMPTY);
if ($characterArray === false) {
return $string;
}

$pattern = '/^[' . preg_quote(implode('', $characterArray), '/') . ']+/u';

return (string) preg_replace($pattern, '', $string);
}
}
4 changes: 3 additions & 1 deletion tests/Fixer/Fixtures/Correct.php.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lmc\CodingStandard\Fixer\Fixtures;

Expand Down
4 changes: 3 additions & 1 deletion tests/Fixer/Fixtures/Fixed.php.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/**
* The function declaration should not be affected
Expand Down
4 changes: 3 additions & 1 deletion tests/Fixer/Fixtures/Wrong.php.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/**
* The function declaration should not be affected
Expand Down
17 changes: 11 additions & 6 deletions tests/Fixer/SpecifyArgSeparatorFixerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Lmc\CodingStandard\Fixer;

Expand All @@ -8,14 +10,17 @@
class SpecifyArgSeparatorFixerTest extends TestCase
{
/**
* @test
* @dataProvider provideFiles
*/
public function shouldFixCode(string $inputFile, string $expectedOutputFile): void
public function testShouldFixCode(string $inputFile, string $expectedOutputFile): void
{
$fixer = new SpecifyArgSeparatorFixer();
$fileInfo = new \SplFileInfo(__DIR__ . '/Fixtures/' . $inputFile);
$tokens = Tokens::fromCode(file_get_contents($fileInfo->getRealPath()));
$fileContents = file_get_contents($fileInfo->getRealPath());
if ($fileContents === false) {
$this->fail('Could not read file ' . $fileInfo->getRealPath());
}
$tokens = Tokens::fromCode($fileContents);

$fixer->fix($fileInfo, $tokens);

Expand All @@ -26,9 +31,9 @@ public function shouldFixCode(string $inputFile, string $expectedOutputFile): vo
}

/**
* @return array[]
* @return array<string, array{string, string}>
*/
public function provideFiles(): array
public static function provideFiles(): array
{
return [
'Correct file should not be changed' => ['Correct.php.inc', 'Correct.php.inc'],
Expand Down
Loading