Skip to content
Open
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
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,37 @@ enforce that the constant values stay api-stable!

#### Default Integration Tests

The trait _BestIt\Sniffs\DefaultSniffIntegrationTestTrait_ provides you with three tests to test the usually use cases of
a sniff based on sniff-individual test files:
Three focused traits provide the standard fixture-based sniff test scenarios. Use only the traits your sniff's fixture
directory actually needs:

- **`BestIt\Sniffs\SniffCorrectFilesTrait`** — provides `testCorrect()`. Use whenever you have a `correct/` fixture folder. Always required.
- **`BestIt\Sniffs\SniffErrorFilesTrait`** — provides `testErrors()`. Use when you have a `with_errors/` fixture folder.
- **`BestIt\Sniffs\SniffWarningFilesTrait`** — provides `testWarnings()`. Use when you have a `with_warnings/` fixture folder.

Example for a sniff that has both errors and warnings:

```php
class MySniffTest extends SniffTestCase
{
use SniffCorrectFilesTrait;
use SniffErrorFilesTrait;
use SniffWarningFilesTrait;
// ...
}
```

Example for a sniff that only has errors:

```php
class MySniffTest extends SniffTestCase
{
use SniffCorrectFilesTrait;
use SniffErrorFilesTrait;
// ...
}
```

1. testCorrect
2. testErrors
3. testWarnings
The tests `testCorrect`, `testErrors`, and `testWarnings` are provided respectively by the above traits.

##### Requirements

Expand Down
30 changes: 14 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"phpcbf"
],
"low-level-checks": [
"license-check",
"phpcpd --fuzzy --verbose src tests",
"phpcs -p src --standard=PHPCompatibility --runtime-set testVersion 8.2",
"phpmd build,src,tests text phpmd.xml.dist",
"phpcpd src tests",
"phpcs --ignore=\"**.js\" --runtime-set ignore_warnings_on_exit 1",
"phpunit --coverage-xml=build/coverage/coverage-xml --log-junit=build/coverage/junit.xml"
],
"high-level-checks": [
"@low-level-checks",
"infection --only-covered -s --no-progress --no-ansi --no-interaction --min-msi=75 --min-covered-msi=75 --skip-initial-tests --coverage=build/coverage --threads=4"
"infection -s --no-progress --no-ansi --no-interaction --min-msi=77 --min-covered-msi=77 --skip-initial-tests --coverage=build/coverage --threads=4"
]
},
"type": "library",
Expand Down Expand Up @@ -45,23 +45,21 @@
},
"prefer-stable": true,
"require": {
"php": ">=8.0",
"slevomat/coding-standard": "^8.0"
"php": ">=8.2",
"slevomat/coding-standard": "^8.0",
"squizlabs/php_codesniffer": "^4.0"
},
"require-dev": {
"phpmd/phpmd": "^2.6",
"sebastian/phpcpd": "^6.0.3",
"phploc/phploc": "^7.0.2",
"phpunit/phpunit": "^9.3",
"phpcompatibility/php-compatibility": "^9.3",
"phpmd/phpmd": "^2.15",
"phpunit/phpunit": "^12.0",
"captainhook/plugin-composer": "^5.3",
"best-it/license-check": "^0.1.0",
"infection/infection": "^0.21.5",
"pdepend/pdepend": "2.8.*"
},
"suggest": {
"phpcompatibility/php-compatibility": "If you want to check for php version compatibility. Please use at least ^9.0."
"infection/infection": "^0.32",
"systemsdk/phpcpd": "^8.3",
"phpcompatibility/php-compatibility": "^10.0.0@dev"
},
"suggest": {
"phpcompatibility/php-compatibility": "If you want to check for php version compatibility. Please use at least ^9.0."
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
Expand Down
13 changes: 0 additions & 13 deletions infection.json.dist

This file was deleted.

19 changes: 19 additions & 0 deletions infection.json5.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"source": {
"directories": [
"src"
]
},
"mutators": {
"@default": true,
"global-ignore": [
"BestIt\\Sniffs\\AbstractSniff::areRequirementsMet",
"BestIt\\Sniffs\\AbstractSniff::fixDefaultProblem",
"BestIt\\Sniffs\\AbstractSniff::setUp",
"BestIt\\Sniffs\\AbstractSniff::tearDown"
],
"global-ignoreSourceCodeByRegex": [
".*->recordMetric.*"
]
}
}
3 changes: 0 additions & 3 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

<rule ref="./src/Standards/BestIt/ruleset.xml" />

<rule ref="PHPCompatibility" />
<config name="testVersion" value="8.0" />

<file>./build/</file>
<file>./src/</file>
<file>./tests/</file>
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php">
<coverage processUncoveredFiles="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.0/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" failOnPhpunitWarning="false">
<source>
<include>
<directory>./src</directory>
</include>
<exclude>
<directory>./tests</directory>
</exclude>
</coverage>
</source>
<testsuites>
<testsuite name="best-it/php_codesniffer">
<directory suffix="Test.php">./tests</directory>
Expand Down
4 changes: 2 additions & 2 deletions src/Standards/BestIt/CodeSniffer/CodeWarning.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public function getStackPosition(): int
/**
* Returns the erroneous token.
*
* @return array The "broken" token.
* @return null|array The "broken" token.
*/
public function getToken(): array
public function getToken(): ?array
{
return $this->token;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Standards/BestIt/CodeSniffer/Helper/DocHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use DomainException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

use function sprintf;

use const T_DOC_COMMENT_CLOSE_TAG;

/**
Expand Down Expand Up @@ -149,7 +151,7 @@ private function loadBlockEndPosition(): ?int
// Search till the next method, property, etc ...
TokenHelper::findPreviousExcluding(
$this->file,
TokenHelper::$ineffectiveTokenCodes + Tokens::$methodPrefixes,
TokenHelper::INEFFECTIVE_TOKEN_CODES + Tokens::METHOD_MODIFIERS,
$this->stackPos - 1,
),
);
Expand Down
60 changes: 31 additions & 29 deletions src/Standards/BestIt/CodeSniffer/Helper/DocTagHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace BestIt\CodeSniffer\Helper;

use PHP_CodeSniffer\Files\File;

use function array_key_exists;
use function in_array;
use function is_int;

use const T_DOC_COMMENT_CLOSE_TAG;
use const T_DOC_COMMENT_STRING;
use const T_DOC_COMMENT_TAG;
Expand Down Expand Up @@ -66,34 +68,27 @@ private function getCommentStartToken(): array
}

/**
* Loads the tag content for the given tag position.
* Returns the individual count of every tag.
*
* @param int $tagPosition The position of the tag.
* @param int $iteratedPosition
* @param array $tagTokens Array of tag tokens.
*
* @return array The content tokens of the tag.
* @return array List of comment tags with there count of the current comment
*/
private function loadTagContentTokens(int $tagPosition, int &$iteratedPosition): array
public function getTagCounts(array $tagTokens): array
{
$contents = [];
$myColumn = $this->tokens[$tagPosition]['column'];
$closingPos = $this->file->findNext([T_DOC_COMMENT_CLOSE_TAG], $position = $tagPosition + 1);

while ($position < $closingPos) {
$contentToken = $this->tokens[$position++];
$tagCounts = [];

if (($contentToken['code'] === T_DOC_COMMENT_TAG) && ($contentToken['column'] <= $myColumn)) {
break;
}
foreach ($tagTokens as $tagToken) {
$tagName = $tagToken['content'];

if (in_array($contentToken['code'], [T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG])) {
$contents[$position] = $contentToken;
if (!array_key_exists($tagName, $tagCounts)) {
$tagCounts[$tagName] = 0;
}

$iteratedPosition = $position;
++$tagCounts[$tagName];
}

return $contents;
return $tagCounts;
}

/**
Expand All @@ -120,26 +115,33 @@ public function getTagTokens(): array
}

/**
* Returns the individual count of every tag.
* Loads the tag content for the given tag position.
*
* @param array $tagTokens Array of tag tokens.
* @param int $tagPosition The position of the tag.
* @param int $iteratedPosition
*
* @return array List of comment tags with there count of the current comment
* @return array The content tokens of the tag.
*/
public function getTagCounts(array $tagTokens): array
private function loadTagContentTokens(int $tagPosition, int &$iteratedPosition): array
{
$tagCounts = [];
$contents = [];
$myColumn = $this->tokens[$tagPosition]['column'];
$closingPos = $this->file->findNext([T_DOC_COMMENT_CLOSE_TAG], $position = $tagPosition + 1);

foreach ($tagTokens as $tagToken) {
$tagName = $tagToken['content'];
while ($position < $closingPos) {
$contentToken = $this->tokens[$position++];

if (!array_key_exists($tagName, $tagCounts)) {
$tagCounts[$tagName] = 0;
if (($contentToken['code'] === T_DOC_COMMENT_TAG) && ($contentToken['column'] <= $myColumn)) {
break;
}

++$tagCounts[$tagName];
if (in_array($contentToken['code'], [T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG])) {
$contents[$position] = $contentToken;
}

$iteratedPosition = $position;
}

return $tagCounts;
return $contents;
}
}
2 changes: 1 addition & 1 deletion src/Standards/BestIt/CodeSniffer/Helper/LineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(File $file, ?Fixer $fixer = null)
*
* @return void
*/
public function removeLine(int $line): void
private function removeLine(int $line): void
{
foreach ($this->file->getTokens() as $tagPtr => $tagToken) {
if ($tagToken['line'] !== $line) {
Expand Down
1 change: 1 addition & 0 deletions src/Standards/BestIt/CodeSniffer/Helper/PropertyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BestIt\CodeSniffer\Helper;

use PHP_CodeSniffer\Files\File;

use function substr;

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Standards/BestIt/CodeSniffer/Helper/TokenHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
*/
class TokenHelper extends BaseHelper
{
public const ARRAY_TOKEN_CODES = [
T_ARRAY,
T_OPEN_SHORT_ARRAY,
];
}
2 changes: 1 addition & 1 deletion src/Standards/BestIt/Sniffs/ClassRegistrationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ trait ClassRegistrationTrait
*/
public function register(): array
{
return Tokens::$ooScopeTokens;
return Tokens::OO_SCOPE_TOKENS;
}
}
2 changes: 2 additions & 0 deletions src/Standards/BestIt/Sniffs/Commenting/AbstractDocSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use BestIt\CodeSniffer\Helper\TokenHelper;
use BestIt\Sniffs\AbstractSniff;
use BestIt\Sniffs\DocPosProviderTrait;

use function ucfirst;

use const T_DOC_COMMENT_OPEN_TAG;
use const T_DOC_COMMENT_STRING;
use const T_DOC_COMMENT_TAG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BestIt\Sniffs\Commenting;

use BestIt\Sniffs\AbstractSniff;

use const T_DOC_COMMENT_STAR;
use const T_DOC_COMMENT_TAG;
use const T_DOC_COMMENT_WHITESPACE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use BestIt\CodeSniffer\CodeError;
use BestIt\CodeSniffer\Helper\TokenHelper;
use BestIt\Sniffs\AbstractSniff;

use function array_key_exists;

use const T_ARRAY;
use const T_CLOSE_PARENTHESIS;
use const T_CLOSE_SHORT_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use BestIt\Sniffs\AbstractSniff;
use BestIt\Sniffs\SuppressingTrait;

use const T_IS_EQUAL;
use const T_IS_NOT_EQUAL;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use BestIt\CodeSniffer\CodeWarning;
use BestIt\CodeSniffer\Helper\TokenHelper;
use BestIt\Sniffs\AbstractSniff;

use function array_merge;

use const T_ASPERAND;
use const T_BOOLEAN_NOT;
use const T_INSTANCEOF;
Expand Down Expand Up @@ -126,7 +128,7 @@ private function getPositionOfTokenBeforeCheck(): int
return TokenHelper::findPreviousExcluding(
$file,
// And skip object property accesses
array_merge(TokenHelper::$ineffectiveTokenCodes, [T_ASPERAND, T_OBJECT_OPERATOR, T_STRING, T_VARIABLE]),
array_merge(TokenHelper::INEFFECTIVE_TOKEN_CODES, [T_ASPERAND, T_OBJECT_OPERATOR, T_STRING, T_VARIABLE]),
$stackPosition - 1,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Standards/BestIt/Sniffs/DocPosProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait DocPosProviderTrait
*
* @return int|bool
*/
protected function getDocCommentPos()
protected function getDocCommentPos(): int|false|null
{
if ($this->docCommentPos === false) {
$this->docCommentPos = $this->loadDocCommentPos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use BestIt\CodeSniffer\Helper\DocTagHelper;
use BestIt\Sniffs\AbstractSniff;
use BestIt\Sniffs\DocPosProviderTrait;

use function in_array;
use function substr;

Expand Down
Loading