spatie/x-ray

快速扫描源代码中调用Ray的方法

1.1.2 2023-12-19 11:08 UTC

This package is auto-updated.

Last update: 2024-09-15 15:49:37 UTC


README

x-ray logo

Package Version license Test Run Status Downloads

本包可以快速扫描源代码中调用ray()rd()Ray::*->ray()辅助方法,这些方法来自spatie/rayspatie/laravel-ray包。

主要用例是在部署前不能在源代码中留下对ray()的调用,即使Ray被禁用。此包不会删除调用,它只是显示它们的存储位置,以便可以手动删除。

如果未找到ray调用,则x-ray命令的退出代码为零,如果找到调用,则非零。这使得可以在如Github Workflows这样的自动化环境中使用此包。

访问myray.app获取下载Ray调试应用的详细信息。

安装

composer require spatie/x-ray --dev

用法

指定一个或多个有效的路径名和/或文件名进行扫描

./vendor/bin/x-ray ./app/Actions/MyAction.php ./app/Models/*.php ./tests --snippets

./src./tests内显示找到的调用的摘要表,同时忽略一些文件

./vendor/bin/x-ray \
  --summary \
  --ignore src/MyClass.php \
  --ignore 'test/fixtures/*.php' \
  ./src ./tests

显示每个文件名及通过/失败状态,以及紧凑的结果

./vendor/bin/x-ray ./app --compact --verbose

可用选项

配置文件

在项目根目录中创建一个名为x-ray.ymlx-ray.yml.dist的文件以配置该包。

如果存在配置文件,则它必须具有根部分functionspaths

每个部分可以有一个ignoreinclude项,两者或都不包含。每个项包含一个字符串数组,如果两者都存在,则包括覆盖忽略项。每个项包含一个字符串数组,如果两者都存在,则包括覆盖忽略项。添加一个*项来忽略或包含所有匹配项(请确保引用星号)

默认情况下,functions.includerayrd函数匹配。

functions:
  include:
    - '*'

paths:
  include:
    - 'tests/fixtures/*'
  ignore:
    - tests
    - 'SettingsTest.php'

自动化

x-ray被设计为不仅用作手动实用工具,而且与自动化工具结合使用。

Github工作流

您可以在github工作流中使用x-ray来确保不要提交任何对ray()的调用。

以下示例工作流使用PHPUnit运行单元测试,然后运行x-ray

name: run-tests

on: 
  push:
    branches:
      - main
  pull_request:

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest]
        php: [8.3, 8.2, 8.1]

    name: P${{ matrix.php }} - ${{ matrix.os }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
          coverage: pcov

      - name: Setup problem matchers
        run: |
          echo "::add-matcher::${{ runner.tool_cache }}/php.json"
          echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      - name: Install dependencies
        run: composer update --prefer-stable --prefer-dist --no-interaction

      - name: Execute tests
        run: ./vendor/bin/phpunit
        
      - name: Check for ray calls
        run: ./vendor/bin/x-ray . --compact

Git钩子

在某些情况下,您可能希望使用git pre-commit钩子来避免提交任何ray()调用

#!/bin/sh

echo "Checking for ray() calls...\n"

x-ray -s .
rayScanExitCode=$?

printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -

localPreCommitExitCode=0
if [ -e ./.git/hooks/pre-commit ]; then
    ./.git/hooks/pre-commit "$@"
    localPreCommitExitCode=$?
fi

exit $rayScanExitCode || $localPreCommitExitCode

您还可以在package.json配置中使用x-ray与husky一起使用

...
"husky": {
    "hooks": {
        "pre-commit": "lint-staged && .x-ray -s ."
    }
},
....

截图

可以在文档中查看各种截图。

测试

./vendor/bin/phpunit

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献以获取详细信息。

安全漏洞

请参阅我们关于如何报告安全漏洞的安全策略

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。