Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update coding standards #226

Merged
merged 6 commits into from
Aug 15, 2023
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
57 changes: 14 additions & 43 deletions ci/php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,31 @@
<?php

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

$finder = PhpCsFixer\Finder::create()
->in([dirname(__DIR__) . '/src', dirname(__DIR__) . '/tests'])
->exclude('unit/fixtures');
->in([dirname(__DIR__) . '/src', dirname(__DIR__) . '/tests']);

$rules = [
'@PSR12' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'@PHP73Migration' => true,
'@PHP74Migration' => true,
'@PHP74Migration:risky' => true,
'@PHP80Migration' => true,
'@PER' => true,
'@PHP80Migration:risky' => true,
'@PHP81Migration' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['default' => 'align_single_space_minimal'],
'align_multiline_comment' => ['comment_type' => 'phpdocs_like'],
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'compact_nullable_typehint' => true,
'escape_implicit_backslashes' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'final_internal_class' => true,
'linebreak_after_opening_tag' => true,
'list_syntax' => ['syntax' => 'short'],
'@PHP82Migration' => true,
'@PhpCsFixer' => true,
PhpCsFixerCustomFixers\Fixer\DeclareAfterOpeningTagFixer::name() => true,
PhpCsFixerCustomFixers\Fixer\PhpdocArrayStyleFixer::name() => true,
PhpCsFixerCustomFixers\Fixer\ConstructorEmptyBracesFixer::name() => true,
'phpdoc_line_span' => ['property' => 'single', 'const' => 'single'],
'phpdoc_separation' => ['groups' => [['deprecated', 'link', 'see', 'since'], ['template', 'template-covariant'], ['param', 'return', 'throws']], 'skip_unlisted_annotations' => true],
'mb_str_functions' => true,
'method_argument_space' => [
'keep_multiple_spaces_after_comma' => false,
'on_multiline' => 'ensure_fully_multiline',
'after_heredoc' => true,
],
'method_chaining_indentation' => true,
'no_php4_constructor' => true,
'no_superfluous_elseif' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'no_unused_imports' => true,
'ordered_imports' => true,
'compact_nullable_typehint' => true,
'single_line_after_imports' => true,
'global_namespace_import' => false,
'php_unit_strict' => true,
'phpdoc_order' => true,
'phpdoc_types_order' => true,
'simplified_null_return' => true,
'strict_comparison' => true,
'strict_param' => true,
'blank_line_before_statement' => ['statements' => ['continue', 'declare', 'exit', 'include', 'include_once', 'phpdoc', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'yield', 'yield_from']],
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
//enable when min php >= 8.0
'get_class_to_class_keyword' => false,
];

return (new PhpCsFixer\Config())
->registerCustomFixers(new PhpCsFixerCustomFixers\Fixers())
->setFinder($finder)
->setRules($rules)
->setRiskyAllowed(true);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.16",
"phpunit/phpunit": "^9.0",
"vimeo/psalm": "^4.0 || ^5.0"
},
Expand Down
8 changes: 3 additions & 5 deletions src/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat;

Expand All @@ -15,9 +13,9 @@ public function __construct(
bool $ignore_built_in_classes,
bool $show_rule_names
) {
$this->ignore_doc_comments = $ignore_doc_comments;
$this->ignore_doc_comments = $ignore_doc_comments;
$this->ignore_built_in_classes = $ignore_built_in_classes;
$this->show_rule_names = $show_rule_names;
$this->show_rule_names = $show_rule_names;
}

public function ignoreDocComments(): bool
Expand Down
5 changes: 1 addition & 4 deletions src/Parser/TypeNodeParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Parser;

Expand Down Expand Up @@ -45,7 +43,6 @@ private static function parseComplex(ComplexType $type, Scope $scope): array
}

/**
* @param NodeAbstract $type
* @return array<Identifier|Name>
*/
private static function flattenType(NodeAbstract $type): array
Expand Down
4 changes: 1 addition & 3 deletions src/Rule/Assertion/Assertion.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion;

Expand Down
4 changes: 1 addition & 3 deletions src/Rule/Assertion/Declaration/Declaration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration;

Expand Down
21 changes: 9 additions & 12 deletions src/Rule/Assertion/Declaration/DeclarationAssertion.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration;

Expand All @@ -18,7 +16,7 @@

abstract class DeclarationAssertion implements Assertion
{
/** @var array<array{string, SelectorInterface, array<SelectorInterface>, string[]}> */
/** @var array<array{string, SelectorInterface, array<SelectorInterface>, array<string>}> */
protected array $statements;
protected Configuration $configuration;
protected ReflectionProvider $reflectionProvider;
Expand All @@ -34,10 +32,10 @@ public function __construct(
ReflectionProvider $reflectionProvider,
FileTypeMapper $fileTypeMapper
) {
$this->statements = $statementBuilderFactory->create($assertion)->build();
$this->configuration = $configuration;
$this->statements = $statementBuilderFactory->create($assertion)->build();
$this->configuration = $configuration;
$this->reflectionProvider = $reflectionProvider;
$this->fileTypeMapper = $fileTypeMapper;
$this->fileTypeMapper = $fileTypeMapper;
}

/**
Expand All @@ -64,33 +62,32 @@ public function prepareMessage(string $ruleName, string $message): string
abstract protected function meetsDeclaration(Node $node, Scope $scope): bool;

/**
* @param string $ruleName
* @param class-string $subject
*/
abstract protected function getMessage(string $ruleName, string $subject): string;

/**
* @param string[] $tips
* @param array<string> $tips
* @return array<RuleError>
*/
abstract protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips): array;

protected function ruleApplies(Scope $scope): bool
{
if (!($scope->isInClass())) {
if (!$scope->isInClass()) {
return false;
}

return $scope->getClassReflection() !== null;
}

/**
* @throws ShouldNotHappenException
* @return array<RuleError>
* @throws ShouldNotHappenException
*/
protected function validateGetErrors(Scope $scope, bool $meetsDeclaration): array
{
$errors = [];
$errors = [];
$subject = $scope->getClassReflection();
if ($subject === null) {
throw new ShouldNotHappenException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldBeAbstract;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldBeAbstract;

Expand Down
4 changes: 1 addition & 3 deletions src/Rule/Assertion/Declaration/ShouldBeFinal/IsFinalRule.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldBeFinal;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldBeFinal;

Expand Down Expand Up @@ -33,10 +31,7 @@ public function __construct(
}

/**
* @param string $ruleName
* @param ClassReflection $subject
* @param bool $meetsDeclaration
* @param string[] $tips
* @param array<string> $tips
* @return array<RuleError>
*/
protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips): array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldBeReadonly;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldBeReadonly;

Expand Down Expand Up @@ -33,10 +31,7 @@ public function __construct(
}

/**
* @param string $ruleName
* @param ClassReflection $subject
* @param bool $meetsDeclaration
* @param string[] $tips
* @param array<string> $tips
* @return array<RuleError>
*/
protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips): array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldNotBeAbstract;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldNotBeAbstract;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldNotBeFinal;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration\ShouldNotBeFinal;

Expand Down
19 changes: 8 additions & 11 deletions src/Rule/Assertion/Declaration/ValidationTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Declaration;

Expand All @@ -13,9 +11,9 @@
trait ValidationTrait
{
/**
* @param string[] $tips
* @throws ShouldNotHappenException
* @param array<string> $tips
* @return array<RuleError>
* @throws ShouldNotHappenException
*/
protected function applyShould(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips): array
{
Expand All @@ -26,7 +24,7 @@ protected function applyShould(string $ruleName, ClassReflection $subject, bool
$this->getMessage($ruleName, $subject->getName())
);

foreach($tips as $tip) {
foreach ($tips as $tip) {
$ruleError->addTip($tip);
}
$errors[] = $ruleError->build();
Expand All @@ -36,9 +34,9 @@ protected function applyShould(string $ruleName, ClassReflection $subject, bool
}

/**
* @param string[] $tips
* @throws ShouldNotHappenException
* @param array<string> $tips
* @return array<RuleError>
* @throws ShouldNotHappenException
*/
protected function applyShouldNot(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips): array
{
Expand All @@ -49,7 +47,7 @@ protected function applyShouldNot(string $ruleName, ClassReflection $subject, bo
$this->getMessage($ruleName, $subject->getName())
);

foreach($tips as $tip) {
foreach ($tips as $tip) {
$ruleError->addTip($tip);
}
$errors[] = $ruleError->build();
Expand All @@ -58,9 +56,8 @@ protected function applyShouldNot(string $ruleName, ClassReflection $subject, bo
return $errors;
}


/**
* @param class-string $classname
* @param class-string $classname
* @param array<SelectorInterface> $targetExcludes
*/
private function nodeMatchesTarget(string $classname, SelectorInterface $target, array $targetExcludes): bool
Expand Down
4 changes: 1 addition & 3 deletions src/Rule/Assertion/Relation/CanOnlyDepend/CanOnlyDepend.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Relation\CanOnlyDepend;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Relation\CanOnlyDepend;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

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

namespace PHPat\Rule\Assertion\Relation\CanOnlyDepend;

Expand Down
Loading
Loading