เลิกใช้แล้ว: php_codesniffer เหมาะสำหรับการจัดการพื้นที่และตำแหน่งถ่าน แต่กฎเหล่านี้เกี่ยวกับสถาปัตยกรรมรหัสและโครงสร้าง ในปี 2020 มีเครื่องมือที่เหมาะกับสิ่งนี้อย่างสมบูรณ์แบบ - phpstan
บอกว่าการเพาะกายวัตถุถูกนำมาใช้เป็นกฎ phpstan ในแพ็คเกจ symplify/phpstan-rules ใช้แทน?
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
การเพาะกายวัตถุเป็น ชุดของกฎในรหัสเชิงวัตถุที่มุ่งเน้นการบำรุงรักษาความสามารถในการอ่านความสามารถในการทดสอบและความเข้าใจ เรา ใช้งานได้ก่อน - พวกเขาใช้งานได้ง่ายเข้าด้วยกันหรือทีละคน
อ่านโพสต์โดย 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.ForbiddenPublicPropertyใน easycodingstandard
# 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.MaxNestingLevelใน ECS:
# 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.NoElseใน ECS:
# 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.OneObjectOperatorPerLineใน ECS:
# 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.ElementNameMinimalLengthใน ECS:
# 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.PropertyPerClassLimitใน ECS:
# 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.NoSetterใน ECS:
# 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. อย่าใช้คลาสที่มีตัวแปรอินสแตนซ์มากกว่าสองตัว ขึ้นอยู่กับแต่ละโดเมนของแต่ละโครงการ มันไม่สมเหตุสมผลที่จะทำกฎสำหรับสิ่งนั้น
1 คุณสมบัติต่อการประชาสัมพันธ์
ทุกคุณสมบัติใหม่ จะต้องครอบคลุมโดยการทดสอบ
การทดสอบและ การตรวจสอบสไตล์ ทั้งหมด จะต้องผ่าน
composer complete-checkเรายินดีที่จะรวมคุณสมบัติของคุณแล้ว