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
37 changes: 21 additions & 16 deletions AntipinCS/Sniffs/Classes/PropertyTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace MaxAntipin\PHPCS\Standards\AntipinCS\Sniffs\Classes;

use Exception;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use PHP_CodeSniffer\Util\Tokens;
Expand All @@ -24,25 +23,31 @@ protected function processMemberVar(File $phpcsFile, int $stackPtr): void
// Detect multiple properties defined at the same time. Throw an error
// for this, but also only process the first property in the list so we don't
// repeat errors.
$find = Tokens::$scopeModifiers;
$find[] = T_VARIABLE;
$find[] = T_VAR;
$find[] = T_READONLY;
$find[] = T_SEMICOLON;
$find[] = T_OPEN_CURLY_BRACKET;

$prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
if ($tokens[$prev]['code'] === T_VARIABLE) {
static $find = [...Tokens::SCOPE_MODIFIERS,
T_VARIABLE,
T_VAR,
T_READONLY,
T_FINAL,
T_ABSTRACT,
T_SEMICOLON,
T_OPEN_CURLY_BRACKET,
];
$prevPtr = $phpcsFile->findPrevious($find, ($stackPtr - 1));
if ($tokens[$prevPtr]['code'] === T_VARIABLE) {
return;
}

try {
$propertyInfo = $phpcsFile->getMemberProperties($stackPtr);
if (empty($propertyInfo)) {
if ($tokens[$prevPtr]['code'] === T_OPEN_CURLY_BRACKET) {
$prevPtr = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, $prevPtr - 1, null, true);
if ($tokens[$prevPtr]['content'] === 'get') {
return;
}
} catch (Exception $e) {
// Turns out not to be a property after all.
}
$nextPtr = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $stackPtr + 1, null, true);
static $exclude = [T_OBJECT_OPERATOR];
if (
in_array($tokens[$nextPtr]['code'], $exclude, true) ||
!($propertyInfo = $phpcsFile->getMemberProperties($stackPtr))
) {
return;
}
$property = $tokens[$stackPtr]['content'];
Expand Down
3 changes: 3 additions & 0 deletions AntipinCS/Sniffs/Functions/ReturnTypeDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function register(): array
public function process(File $phpcsFile, int $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
/** @var array<string,int|string> $token */
$token = $tokens[$stackPtr];
if (!isset($token['parenthesis_opener']) || !isset($token['parenthesis_closer'])) {
$phpcsFile->addError(
Expand All @@ -36,7 +37,9 @@ public function process(File $phpcsFile, int $stackPtr): void
);
return;
}
/** @var array<string,bool> $skipTypes */
static $skipTypes = ['T_CLOSURE' => true, 'T_FN' => true];
/** @var array<string,bool> $skipMethods */
static $skipMethods = ['__construct' => true, '__destruct' => true];
if (
!isset($skipTypes[$token['type']])
Expand Down
44 changes: 44 additions & 0 deletions AntipinCS/Tests/Classes/PropertyTypeDeclarationUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file is auto-generated. DO NOT EDIT.
*/

declare(strict_types=1);

namespace MaxAntipin\YandexCloud\SDK\Service;

use MaxAntipin\YandexCloud\SDK\Service\Resource\DNS\DnsFirewallResource;
use MaxAntipin\YandexCloud\SDK\Service\Resource\DNS\DnsInboundEndpointResource;
use MaxAntipin\YandexCloud\SDK\Service\Resource\DNS\DnsZoneResource;
use Thesis\Grpc\Client as GRPCClient;
use Yandex\Cloud\Dns\V1\DnsFirewallServiceClient;
use Yandex\Cloud\Dns\V1\DnsInboundEndpointServiceClient;
use Yandex\Cloud\Dns\V1\DnsZoneServiceClient;

final class DNSService implements APIServiceInterface
{
public private(set) DnsFirewallResource $dnsFirewall {
get {
return $this->dnsFirewall ??= new DnsFirewallResource(new DnsFirewallServiceClient($this->client));
}
}

public private(set) DnsInboundEndpointResource $dnsInboundEndpoint {
get {
$a = 5;
return $this->dnsInboundEndpoint ??= new DnsInboundEndpointResource(
new DnsInboundEndpointServiceClient($this->client)
);
}
}

public private(set) DnsZoneResource $dnsZone {
get => $this->dnsZone ??= new DnsZoneResource(new DnsZoneServiceClient($this->client));
}

public function __construct(
private readonly GRPCClient $client,
) {
}
}
22 changes: 19 additions & 3 deletions AntipinCS/Tests/Classes/PropertyTypeDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
#[CoversClass(PropertyTypeDeclarationSniff::class)]
final class PropertyTypeDeclarationUnitTest extends AbstractSniffTestCase
{
/**
* Get a list of all test files to check.
*
* @param string $testFileBase The base path that the unit tests files will have.
*
* @return string[]
*/
protected function getTestFiles(string $testFileBase): array
{
return array_map(static fn (int $i): string => $testFileBase . $i . '.inc', range(1, 2));
}

/**
* Returns the lines where errors should occur.
*
Expand All @@ -25,9 +37,10 @@ final class PropertyTypeDeclarationUnitTest extends AbstractSniffTestCase
*
* @return array<int, int>
*/
public function getErrorList(): array
public function getErrorList(string $testFile = ''): array
{
return [
return match ($testFile) {
'PropertyTypeDeclarationUnitTest.1.inc' => [
4 => 1,
5 => 1,
6 => 1,
Expand All @@ -45,7 +58,10 @@ public function getErrorList(): array
37 => 1,
38 => 1,
51 => 1,
];
],
'PropertyTypeDeclarationUnitTest.2.inc' => [],
default => throw new \RuntimeException('Unhandled test file: ' . $testFile)
};
}

/**
Expand Down
39 changes: 0 additions & 39 deletions AntipinCS/Tests/Functions/ReturnTypeDeclarationUnitTest.2.inc

This file was deleted.

30 changes: 7 additions & 23 deletions AntipinCS/Tests/Functions/ReturnTypeDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,18 @@
#[CoversClass(ReturnTypeDeclarationSniff::class)]
final class ReturnTypeDeclarationUnitTest extends AbstractSniffTestCase
{
/**
* Get a list of all test files to check.
*
* @param string $testFileBase The base path that the unit tests files will have.
*
* @return string[]
*/
protected function getTestFiles(string $testFileBase): array
{
return array_map(static fn (int $i): string => $testFileBase . $i . '.inc', range(1, 2));
}

/**
* @return array<int, int>
*/
protected function getErrorList(string $testFile = ''): array
{
return match ($testFile) {
'ReturnTypeDeclarationUnitTest.1.inc' => [
65 => 1,
68 => 1,
73 => 1,
75 => 1,
77 => 1,
],
'ReturnTypeDeclarationUnitTest.2.inc' => [],
default => throw new \RuntimeException('Unhandled test file: ' . $testFile)
};
return [
65 => 1,
68 => 1,
73 => 1,
75 => 1,
77 => 1,
];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ check-dockerfile:
docker run --rm -i ghcr.io/hadolint/hadolint < .docker/Dockerfile

lint:
php ./vendor/bin/phpcs
php ./vendor/bin/phpcs -v -s
php ./vendor/bin/phpstan analyze
php ./vendor/bin/phpcs-check-feature-completeness

fix-lint:
php ./vendor/bin/phpcbf

test-cs:
cd ../cs-test/ && php ./vendor/bin/phpunit --no-coverage -v -s --filter AntipinCS
cd ../cs-test/ && php ./vendor/bin/phpunit --no-coverage --filter AntipinCS

test-coverage:
cd ../cs-test/ && XDEBUG_MODE=coverage php ./vendor/bin/phpunit --filter AntipinCS
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ Two errors with property hooks:
- Missing type declaration for property "$this"

```Shell
cat AntipinCS/Tests/Functions/ReturnTypeDeclarationUnitTest.2.inc | ./vendor/bin/phpcs --stdin-path=/php-code-sniffs/AntipinCS/Tests/Functions/ReturnTypeDeclarationUnitTest.2.php -v -s -
cat AntipinCS/Tests/Classes/PropertyTypeDeclarationUnitTest.2.inc | ./vendor/bin/phpcs --stdin-path=/php-code-sniffs/AntipinCS/Tests/Classes/PropertyTypeDeclarationUnitTest.2.php -v -s -
```
Loading