非推奨:PHP_CODESNIFFERは、スペースとCharの位置を処理するのに最適です。しかし、これらのルールはコードアーキテクチャと構造に関するものです。 2020年には、これに完全に適したツール-Phpstanがあります。
そうは言っても、オブジェクトの体操はsymplify/phpstan-rulesパッケージでphpstanルールとして実装されました。代わりにそれを使用しますか?
includes:
- vendor/symplify/phpstan-rules/packages/object-calisthenics/config/object-calisthenics-rules.neon
- vendor/symplify/phpstan-rules/packages/object-calisthenics/config/object-calisthenics-services.neon
オブジェクトの体操は、オブジェクト指向のコードのルールのセットであり、保守性、読みやすさ、テスト可能性、および理解に焦点を当てています。私たちは最初に実用的です - それらはすべて一緒に、または1つずつ使いやすいです。
William Durandによる投稿を読むか、 Guilherme Blancoによるプレゼンテーションをチェックしてください。
composer require object-calisthenics/phpcs-calisthenics-rules --devあなたが望むものを知っているなら、特定のルールにすぐにジャンプしてください:
php_codesnifferで
vendor/bin/phpcs src tests -sp
--standard=vendor/object-calisthenics/phpcs-calisthenics-rules/src/ObjectCalisthenics/ruleset.xml
--sniffs=ObjectCalisthenics.Classes.ForbiddenPublicPropertyEasyCodingStandardで
# ecs.yaml
services :
ObjectCalisthenicsSniffsClassesForbiddenPublicPropertySniff : ~それから
vendor/bin/ecs check srcXレベルのみ foreach ( $ sniffGroups as $ sniffGroup ) {
foreach ( $ sniffGroup as $ sniffKey => $ sniffClass ) {
if (! $ sniffClass instanceof Sniff) {
throw new InvalidClassTypeException ;
}
}
}?
foreach ( $ sniffGroups as $ sniffGroup ) {
$ this -> ensureIsAllInstanceOf ( $ sniffGroup , Sniff::class);
}
// ...
private function ensureIsAllInstanceOf ( array $ objects , string $ type )
{
// ...
}php_codesniffer:
vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Metrics.MaxNestingLevelECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsMetricsMaxNestingLevelSniff : ~ php_codesniffer:
<? xml version = " 1.0 " ?>
< ruleset name = " my-project " >
< rule ref = " ObjectCalisthenics.Metrics.MaxNestingLevel " >
< properties >
< property name = " maxNestingLevel " value = " 2 " />
</ properties >
</ rule >
</ ruleset >ECS:
services :
ObjectCalisthenicsSniffsMetricsMaxNestingLevelSniff :
maxNestingLevel : 2 if ( $ status === self :: DONE ) {
$ this -> finish ();
} else {
$ this -> advance ();
}?
if ( $ status === self :: DONE ) {
$ this -> finish ();
return ;
}
$ this -> advance ();php_codesniffer:
vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.ControlStructures.NoElseECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsControlStructuresNoElseSniff : ~-> )のみを使用します $ this -> container -> getBuilder ()-> addDefinition (SniffRunner::class);?
$ containerBuilder = $ this -> getContainerBuilder ();
$ containerBuilder -> addDefinition (SniffRunner::class);php_codesniffer:
vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLineECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsCodeAnalysisOneObjectOperatorPerLineSniff : ~ php_codesniffer:
<? xml version = " 1.0 " ?>
< ruleset name = " my-project " >
< rule ref = " ObjectCalisthenics.CodeAnalysis.OneObjectOperatorPerLine " >
< properties >
< property name = " variablesHoldingAFluentInterface " type = " array " value = " $queryBuilder,$containerBuilder " />
< property name = " methodsStartingAFluentInterface " type = " array " value = " createQueryBuilder " />
< property name = " methodsEndingAFluentInterface " type = " array " value = " execute,getQuery " />
</ properties >
</ rule >
</ ruleset >ECS:
services :
ObjectCalisthenicsSniffsCodeAnalysisOneObjectOperatorPerLineSniff :
variablesHoldingAFluentInterface : ["$queryBuilder", "$containerBuilder"]
methodsStartingAFluentInterface : ["createQueryBuilder"]
methodsEndingAFluentInterface : ["execute", "getQuery"]これは、クラス、特性、インターフェイス、定数、関数、変数名に関連しています。
class EM
{
// ...
}?
class EntityMailer
{
// ...
}php_codesniffer:
vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.NamingConventions.ElementNameMinimalLengthECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsNamingConventionsElementNameMinimalLengthSniff : ~ php_codesniffer:
<? xml version = " 1.0 " ?>
< ruleset name = " my-project " >
< rule ref = " ObjectCalisthenics.NamingConventions.ElementNameMinimalLength " >
< properties >
< property name = " minLength " value = " 3 " />
< property name = " allowedShortNames " type = " array " value = " i,id,to,up " />
</ properties >
</ rule >
</ ruleset >ECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsNamingConventionsElementNameMinimalLengthSniff :
minLength : 3
allowedShortNames : ["i", "id", "to", "up"] class SimpleStartupController
{
// 300 lines of code
}?
class SimpleStartupController
{
// 50 lines of code
} class SomeClass
{
public function simpleLogic ()
{
// 30 lines of code
}
}?
class SomeClass
{
public function simpleLogic ()
{
// 10 lines of code
}
} class SomeClass
{
// 20 properties
}?
class SomeClass
{
// 5 properties
} class SomeClass
{
// 20 methods
}?
class SomeClass
{
// 5 methods
}php_codesniffer:
vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Files.ClassTraitAndInterfaceLength,ObjectCalisthenics.Files.FunctionLength,ObjectCalisthenics.Metrics.MethodPerClassLimit,ObjectCalisthenics.Metrics.PropertyPerClassLimitECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsFilesClassTraitAndInterfaceLengthSniff : ~
ObjectCalisthenicsSniffsFilesFunctionLengthSniff : ~
ObjectCalisthenicsSniffsMetricsMethodPerClassLimitSniff : ~
ObjectCalisthenicsSniffsMetricsPropertyPerClassLimitSniff : ~ php_codesniffer:
<? xml version = " 1.0 " ?>
< ruleset name = " my-project " >
< rule ref = " ObjectCalisthenics.Files.ClassTraitAndInterfaceLength " >
< properties >
< property name = " maxLength " value = " 200 " />
</ properties >
</ rule >
< rule ref = " ObjectCalisthenics.Files.FunctionLength " >
< properties >
< property name = " maxLength " value = " 20 " />
</ properties >
</ rule >
< rule ref = " ObjectCalisthenics.Metrics.PropertyPerClassLimit " >
< properties >
< property name = " maxCount " value = " 10 " />
</ properties >
</ rule >
< rule ref = " ObjectCalisthenics.Metrics.MethodPerClassLimit " >
< properties >
< property name = " maxCount " value = " 10 " />
</ properties >
</ rule >
</ ruleset >ECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsFilesClassTraitAndInterfaceLengthSniff :
maxLength : 200
ObjectCalisthenicsSniffsFilesFunctionLengthSniff :
maxLength : 20
ObjectCalisthenicsSniffsMetricsPropertyPerClassLimitSniff :
maxCount : 10
ObjectCalisthenicsSniffsMetricsMethodPerClassLimitSniff :
maxCount : 10このルールは、ドメイン駆動型の設計に部分的に関連しています。
class ImmutableBankAccount
{
public $ currency = ' USD ' ; private $ amount ;
public function setAmount ( int $ amount )
{
$ this -> amount = $ amount ;
}
}?
class ImmutableBankAccount
{
private $ currency = ' USD ' ; private $ amount ;
public function withdrawAmount ( int $ withdrawnAmount )
{
$ this -> amount -= $ withdrawnAmount ;
}
}php_codesniffer:
vendor/bin/phpcs ... --sniffs=ObjectCalisthenics.Classes.ForbiddenPublicProperty,ObjectCalisthenics.NamingConventions.NoSetterECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsClassesForbiddenPublicPropertySniff : ~
ObjectCalisthenicsSniffsNamingConventionsNoSetterSniff : ~ php_codesniffer:
<? xml version = " 1.0 " ?>
< ruleset name = " my-project " >
< rule ref = " ObjectCalisthenics.NamingConventions.NoSetter " >
< properties >
< property name = " allowedClasses " type = " array " value = " *DataObject " />
</ properties >
</ rule >
</ ruleset >ECS:
# ecs.yaml
services :
ObjectCalisthenicsSniffsNamingConventionsNoSetterSniff :
allowedClasses :
- ' *DataObject '実際に使用している間、これらのルールは、よりクリーンでより実用的なコードの書き込みを支援するのではなく、あまりにも厳格すぎる、曖昧または迷惑でさえあることがわかりました。また、ドメイン駆動型のデザインと密接に関連しています。
3。プリミティブタイプと文字列- PHP 7以降、 define(strict_types=1)およびスカラータイプのヒントを使用できます。他の場合、たとえば電子メールでは、値オブジェクトを介してドメインでそれを扱うことができます。
4。ファーストクラスのコレクションを使用します- このルールは理にかなっていますが、実際には有用ではありません。私たちのコードでさえ、まったく渡されませんでした。
8. 3つ以上のインスタンス変数を持つクラスを使用しないでください。これは、各プロジェクトの個々のドメインに依存します。そのためのルールを作成することは意味がありません。
PRごとに1つの機能
すべての新しい機能は、テストでカバーする必要があります
すべてのテストとスタイルチェックが合格する必要があります
composer complete-checkその場合、私たちはあなたの機能を融合させて喜んでいます。