vantoozz / phpcdm
PHP代码密度计(PHPCDM)
v1.1.1
2020-02-16 16:30 UTC
Requires
- symfony/console: ^3.2|~4|~5
- symfony/event-dispatcher: ~3|~4|~5
- symfony/finder: ~3|~4|~5
Requires (Dev)
- mikey179/vfsstream: ~1.6
This package is auto-updated.
Last update: 2024-09-05 21:06:24 UTC
README
phpcdm
是一个用于 PHP 的代码密度计
什么是代码密度?
看看这段代码
<?php final class SomethingGreat { public function doSomethingGreat($entity, $property, $type, $refProperties, $update, $delete) { $time = $this->startAction(" $type (" . implode(',', (array) $type) . ')'); $this->createAction()->addExtraKey($entity, $entity, $property, $refProperties, $delete, $update)->execute(); $this->finishAction($time); $this->createAction()->updateProperty($entity, $property, $type)->execute(); if ($type instanceof InvalidArgumentException && $type->getMessage() !== null) { $this->createAction()->addDescriptionOnProperty($entity, $property, $type->description)->execute(); } $this->createAction()->renameEntity($entity, $property)->execute(); $this->finishAction($time); $time = $this->startAction(" $type (" . implode(',', (array) $type) . ')'); $this->createAction()->addExtraKey($entity, $entity, $property, $refProperties, $delete, $update)->execute(); if ($type instanceof InvalidArgumentException && $type->getMessage() !== $delete) { $this->finishAction($time); $this->createAction()->addDescriptionOnProperty($entity, $property, $type->description)->execute(); $this->createAction()->renameEntity($entity, $property)->execute(); } $this->createAction()->updateProperty($entity, $property, $type)->execute(); $this->finishAction($time); } }
看起来像一堵字符墙。实际上 确实是 一堵字符墙。没有人能轻易理解这段代码作者的意图,肯定也不会有人愿意尝试去阅读它。此外,这样的字符墙往往是不良应用程序设计的产物。
代码密度 是衡量单页面上字符数量的指标。
PHP 代码密度计旨在帮助开发者防止代码密度问题,并最终保持应用程序的良好状态。
示例代码的密度为 0.381,远高于默认阈值(0.2)
安装
您可以使用 Composer 将此工具添加到您的项目中,作为本地、项目特定、开发时依赖项
composer require --dev vantoozz/phpcdm
此外,PHPCDM 也可以全局安装,如下所示
composer global require --dev vantoozz/phpcdm
运行
分析 src
目录
./vendor/bin/phpcdm src
以错误代码退出
./vendor/bin/phpcdm src --non-zero-exit-on-violation