定制 moxio/php-codesniffer-sniffs 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

moxio/php-codesniffer-sniffs

Composer 安装命令:

composer require moxio/php-codesniffer-sniffs

包简介

Custom sniffs for PHP_CodeSniffer

README 文档

README

Continuous Integration Latest Stable Version

Moxio PHP_CodeSniffer sniffs

This is a collection of our custom PHP_Codesniffer (3.x) sniffs for detecting potential bugs and unexpected behavior in PHP code. It may be used as a ruleset on its own, but it is mainly intended as a set of separate sniffs that can be integrated into other standards.

We described the motivation behind some of these sniffs on our blog.

Installation and usage

Install as a development dependency using composer:

$ composer require --dev moxio/php-codesniffer-sniffs

Check your files against this set of sniffs:

$ ./vendor/bin/phpcs --standard=vendor/moxio/php-codesniffer-sniffs/MoxioSniffs path/to/your/files

Description of sniffs

More sniffs will be added soon.

MoxioSniffs.PHP.DisallowBareContinueInSwitch: Disallows the continue statement without a numeric argument when used directly within a switch-case. This prevents silent bugs caused by PHP considering switch to be a looping structure.

MoxioSniffs.PHP.DisallowImplicitLooseComparison: Disallows implicit non-strict comparisons by functions like in_array and array_search. Requires that the $strict-parameter to these functions is explicitly set. This prevents hidden bugs due to counter-intuitive behavior of non-strict comparison.

MoxioSniffs.PHP.DisallowImplicitLooseBase64Decode: Disallows implicit non-strict usage of the base64_decode function. Requires that the $strict-parameter to this function is explicitly set.

MoxioSniffs.PHP.DisallowUniqidWithoutMoreEntropy: Disallows calls to uniqid() without $more_entropy = true. When $more_entropy is false (which is the default), uniqid() calls usleep() to avoid collisions, which can be a substantial performance hit. Always calling uniqid() with $more_entropy = true avoids these problems.

MoxioSniffs.PHP.DisallowArrayCombinersWithSingleArray: Disallows calls to functions that combine two or more arrays with only a single array given as an argument. This applies to functions like array_merge(_recursive), array_replace(_recursive) and all variants of array_diff and array_intersect. Such a call does not make sense, and is most likely a result of a misplaced comma or parenthesis. To re-index a single array, just use array_values.

MoxioSniffs.PHP.DisallowImplicitMicrotimeAsString: Disallows calls to microtime() without the $get_as_float argument being explicitly set. By default, microtime has a string as its return value ("msec sec"), which is unexpected and cannot be naively cast to float, making it error-prone. It is still possible to set this argument to false, but in that case you have probably thought about this.

MoxioSniffs.PHP.DisallowImplicitIteratorToArrayWithUseKeys: Disallows calls to iterator_to_array() without the $use_keys argument being explicitly set. By default, iterator_to_array uses the keys provided by the iterator. This behavior is often desired for associative arrays, but can cause unexpected results for 'list-like' arrays. Explicitly requiring the parameter to be set ensures that the developer has to think about which behavior is desired for the situation at hand.

MoxioSniffs.PHP.DisallowDateTime: Disallows usage of \DateTime and promotes the use of \DateTimeImmutable instead. The former being mutable can lead to some subtle but nasty bugs. See this post for more background on why you would want to discourage using \DateTime.

MoxioSniffs.PHP.DisallowMbDetectEncoding: Disallows usage of mb_detect_encoding. This function has a misleading name that implies it can actually detect the encoding of a string, a problem which is generally impossible. Rather it checks a list of encodings until it finds one that could be the right one (i.e. the string is a valid byte sequence according to that encoding). Using mb_check_encoding (possibly in a loop) instead makes this much more explicit. See this talk for more background information on this topic.

MoxioSniffs.PHP.DisallowUtf8EncodeDecode: Disallows calls to utf8_encode() and utf8_decode(). These functions can be considered misleading because they only convert to/from ISO-8859-1, and do not 'magically' detect the source/target encoding. Using iconv() or mb_convert_encoding() instead makes both character encodings that play a role in the conversion explicit.

MoxioSniffs.PHP.DisallowDateCreateFromFormatWithUnspecifiedTimeComponent: Disallows calls to \DateTime::createFromFormat, \DateTimeImmutable::createFromFormat, date_create_from_format & date_create_immutable_from_format with formats which do not specify a time component and do not initialize fields to null. This would otherwise create DateTime(Immutable) objects with a time component set to the current (creation) time, which is probably never what you want and can be a source of bugs.

Running tests

After installing dependencies (including development dependencies) using Composer, run

$ ./vendor/bin/phpunit

from the project root dir.

Versioning

This project adheres to Semantic Versioning.

Please note that, from the perspective of this library as a pick-and-match collection of sniffs (and not a complete coding standard), the addition of new sniffs will not be considered a breaking change and thus does not cause an increase in the major version number.

License

These sniffs are released under the MIT license.

moxio/php-codesniffer-sniffs 适用场景与选型建议

moxio/php-codesniffer-sniffs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 293.36k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2016 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「standards」 「phpcs」 「PHP_CodeSniffer」 「sniffs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 moxio/php-codesniffer-sniffs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 moxio/php-codesniffer-sniffs 我们能提供哪些服务?
定制开发 / 二次开发

基于 moxio/php-codesniffer-sniffs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 293.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 23
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 13
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-23