当前位置: 首页 > news >正文

php质量工具系列之paslm

Psalm是一个静态分析工具,深入程序,尽可能多地找到与类型相关的bug
混合类型警告
Intelligent logic checks
属性初始化检查
Taint analysis
Language Server
Automatic fixes
Automatic refactoring

安装

composer global require --dev vimeo/psalm 
# 当使用这个方式安装时,如果psalm的依赖和项目依赖有冲突,可使用phar安装
composer global require --dev psalm/phar

注意:
linux上,需要将全局composer的可执行目录加入环境变量,目录路径一般是/tmp/composer/vendor/bin
环境变量配置

windows上,只需在环境变量path中加上composer路径,目录路径一般是
%APPDATA%\Composer\vendor\bin

初始化

./vendor/bin/psalm --init

psalm扫描你的项目,评估适合项目的错误级别,并生成psalm.xml文件用于后续的扫描配置

执行

./vendor/bin/psalm#或者扫描某个或某些文件
./vendor/bin/psalm file1.php [file2.php...]
./vendor/bin/psalm --show-info=true

参数解释:
–show-info 显示非异常的分析结果

#针对特定问题给出psalm的解决方案,可参考其结果进行代码修复
./vendor/bin/psalm --alter --issues=MissingReturnType --dry-run

参数解释:
–issues all 显示全部问题解决方案;其他问题type,多个间以逗号分隔,如 MissingReturnType,InvalidArgument

插件

查看可用插件列表

composer search -t psalm-plugin '.'

安装

composer global require --dev <plugin-vendor/plugin-package>

启用

vendor/bin/psalm-plugin enable plugin-vendor/plugin-package

禁用

vendor/bin/psalm-plugin disable plugin-vendor/plugin-package

显示已安装列表

vendor/bin/psalm-plugin show

配置

以下是我的配置,大家可以根据情况选择配置

<?xml version="1.0"?>
<psalmerrorLevel="4"resolveFromConfigFile="true"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="https://getpsalm.org/schema/config"xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"findUnusedBaselineEntry="true"findUnusedCode="true"autoloader="psalm_autoloader.php"allowStringToStandInForClass="true"
><!-- 添加错误忽略配置 --><issueHandlers><DocblockTypeContradiction><errorLevel type="suppress"><directory name="app"/><!--<file name="path/to/file_with_unused_methods.php"/>--></errorLevel></DocblockTypeContradiction><NonInvariantDocblockPropertyType><errorLevel type="suppress"><directory name="app"/></errorLevel></NonInvariantDocblockPropertyType><PossiblyUndefinedArrayOffset><errorLevel type="suppress"><directory name="app"/></errorLevel></PossiblyUndefinedArrayOffset><PossiblyInvalidOperand><errorLevel type="suppress"><directory name="app"/></errorLevel></PossiblyInvalidOperand><PossiblyUndefinedVariable><errorLevel type="suppress"><directory name="app"/></errorLevel></PossiblyUndefinedVariable><RiskyTruthyFalsyComparison><errorLevel type="suppress"><directory name="app"/></errorLevel></RiskyTruthyFalsyComparison><PossiblyInvalidMethodCall><errorLevel type="suppress"><directory name="app/model"/></errorLevel></PossiblyInvalidMethodCall><MissingReturnType><errorLevel type="suppress"><directory name="app/model"/></errorLevel></MissingReturnType><PossiblyNullOperand><errorLevel type="suppress"><directory name="app/model"/></errorLevel></PossiblyNullOperand><PossiblyNullArgument><errorLevel type="suppress"><directory name="app/model"/></errorLevel></PossiblyNullArgument><PropertyNotSetInConstructor><errorLevel type="suppress"><directory name="app"/></errorLevel></PropertyNotSetInConstructor><UnusedClass><errorLevel type="suppress"><directory name="app"/></errorLevel></UnusedClass><PossiblyUnusedMethod><errorLevel type="suppress"><directory name="app"/></errorLevel></PossiblyUnusedMethod><UndefinedConstant><errorLevel type="suppress"><directory name="app"/><directory name="config"/></errorLevel></UndefinedConstant><UnusedConstructor><errorLevel type="suppress"><file name="app/redis"/></errorLevel></UnusedConstructor><UnusedMethod><errorLevel type="suppress"><file name="app/common"/></errorLevel></UnusedMethod><UnusedFunctionCall><errorLevel type="suppress"><file name="app/common.php"/></errorLevel></UnusedFunctionCall><PossiblyUnusedProperty><errorLevel type="suppress"><file name="app/BaseController.php"/></errorLevel></PossiblyUnusedProperty><InvalidArrayOffset><errorLevel type="suppress"><file name="app/common.php"/></errorLevel></InvalidArrayOffset><InvalidArgument><errorLevel type="suppress"><file name="config/database.php"/></errorLevel></InvalidArgument></issueHandlers><!-- 配置需要扫描的范围,以及需要忽略的目录和文件 --><projectFiles><directory name="app" /><directory name="config" /><ignoreFiles><directory name="vendor" /><directory name="test" /><file name="app/command/Swagger.php" /></ignoreFiles></projectFiles>
</psalm>

文档解释:
issueHandlers 中是扫描问题的类型,其中directory 是需要忽略问题的目录,file是需要忽略问题的文件

自动化构建(Jenkins)中使用

参考文章 jenkins插件之Warnings 项目配置一章中的psalm章节

相关文章:

  • 工博科技联手伯尼纳,共谋食品包装外贸行业新市场,助力全球市场拓展!
  • 质量源于设计:QbD培训引领企业产品质量飞跃!
  • 数据库编程
  • 周报 | 24.5.20-24.5.26文章汇总
  • Python函数式编程入门窥探
  • 高弹性架构的微服务设计模式
  • 什么样的跨网文件交换系统适合车企行业?
  • 【Javascript】Promise形象比喻
  • Java的逻辑控制和方法的使用介绍
  • springboot+minio 文件上传
  • 【随笔4】心情——复杂
  • 一个普通双非女生的秋招之路
  • 轻松学EntityFramework Core--Entity Framework Core 简介
  • 重生之 SpringBoot3 入门保姆级学习(06、属性绑定)
  • c语言如何向文件写入字符串
  • 2019.2.20 c++ 知识梳理
  •  D - 粉碎叛乱F - 其他起义
  • ESLint简单操作
  • gitlab-ci配置详解(一)
  • js面向对象
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • Sublime text 3 3103 注册码
  • 马上搞懂 GeoJSON
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 区块链分支循环
  • 使用common-codec进行md5加密
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 项目实战-Api的解决方案
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • 正则表达式小结
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • LIGO、Virgo第三轮探测告捷,同时探测到一对黑洞合并产生的引力波事件 ...
  • ​flutter 代码混淆
  • #Linux(make工具和makefile文件以及makefile语法)
  • $ git push -u origin master 推送到远程库出错
  • (003)SlickEdit Unity的补全
  • (11)MATLAB PCA+SVM 人脸识别
  • (2)空速传感器
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (六)DockerCompose安装与配置
  • (十二)Flink Table API
  • (贪心) LeetCode 45. 跳跃游戏 II
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET Framework 4.6.2改进了WPF和安全性
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .net中我喜欢的两种验证码
  • .pyc文件是什么?
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析
  • @Slf4j idea标红Cannot resolve symbol ‘log‘
  • [ Socket学习 ] 第一章:网络基础知识
  • [100天算法】-目标和(day 79)
  • [C++]类和对象(中)