定制 ikabalzam/laravel-xray 二次开发

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

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

ikabalzam/laravel-xray

Composer 安装命令:

composer require ikabalzam/laravel-xray

包简介

Static analysis for Eloquent column references — finds invalid database column names in your Laravel code before they hit production.

README 文档

README

Static analysis for Eloquent column references. Finds invalid database column names in your Laravel code before they hit production.

Xray uses PHP-Parser to build an AST of your codebase, then traces every ->where('column'), ->orderBy('column'), ->pluck('column') (and 30+ other methods) back to its database table and validates the column actually exists.

The Problem

// This will blow up at runtime — 'stauts' doesn't exist
User::where('stauts', 'active')->get();

// This passes all tests until a specific code path runs
$query->orderBy('craeted_at');

// Renamed a column in a migration but missed a reference
Invoice::where('total', '>', 1000); // was renamed to 'total_in_cents'

These bugs survive code review, pass your test suite, and explode in production. Xray catches them statically — no HTTP requests, no test data, no runtime needed.

Installation

composer require ikabalzam/laravel-xray --dev

Laravel auto-discovers the service provider. No manual registration needed.

Usage

# Full audit
php artisan xray:audit

# Audit a specific table
php artisan xray:audit --table=users

# Show suggested fixes for typos
php artisan xray:audit --fix

# JSON output (for CI pipelines)
php artisan xray:audit --json

# Show unresolved references (dynamic class/table names)
php artisan xray:audit --show-unresolved

# Scan a specific directory
php artisan xray:audit --path=app/Services

What It Catches

  • Typos: 'stauts' instead of 'status' (with Levenshtein suggestions)
  • Renamed columns: References to columns that no longer exist after a migration
  • Wrong table: Column exists but on a different table than the one being queried
  • Copy-paste errors: Column from one model accidentally used in another's query

What It Understands

Xray isn't a dumb regex grep. It performs deep AST analysis:

  • Method chains: User::where()->orderBy()->get() — traces the chain back to User
  • Relationships: $this->posts()->where('title', ...) — resolves posts() to the posts table
  • Closures: ->whereHas('posts', fn($q) => $q->where('title', ...)) — resolves through closure context
  • Nested closures: Multiple closure levels deep
  • Variable tracking: $query = User::query(); $query->where('name', ...) — traces variable assignments
  • Self-referencing: $q = $q->where(...) — traces back to the original assignment
  • Static calls: User::where(), self::where(), static::where(), DB::table('users')
  • Collection detection: Won't flag $users->where('name', ...) — it knows that's Collection::where(), not Builder::where()
  • Resources: $this->status in a Resource file resolves to the underlying model
  • Scopes: scopeActive($query) — knows $query is a Builder for this model
  • Raw SQL: Extracts columns from selectRaw('COUNT(id) as total')
  • SQL aliases: Won't flag columns defined with AS alias elsewhere in the file
  • Type hints: function foo(Collection $items) — knows to skip Collection parameters
  • @audit-skip: Opt out of specific lines, methods, or entire classes

Suppressing False Positives

// Inline
->where('dynamic_column', $value) // @audit-skip

// Line above
// @audit-skip — this column exists in a dynamic view
->where('computed_field', $value)

// Entire method
/** @audit-skip This method uses a legacy table not in the main schema */
public function legacyQuery() {
    // All column references in this method are skipped
}

Configuration

php artisan vendor:publish --tag=xray-config
// config/xray.php
return [
    // Default scan path
    'path' => app_path(),

    // Where your models live
    'models_path' => app_path('Models'),

    // Extra column patterns to ignore (regex)
    'ignored_column_patterns' => [
        '/^cached_/',  // your custom virtual attributes
    ],

    // Extra methods to treat as non-relationship during chain walking
    'non_relation_methods' => [
        'applyFilters',  // your custom Builder macros
    ],

    // Extra Collection-only methods (NEVER add Builder methods here!)
    'collection_only_methods' => [],
];

CI Integration

Xray returns exit code 1 when issues are found, making it perfect for CI:

# GitHub Actions
- name: Audit column references
  run: php artisan xray:audit --json
# GitLab CI
xray:
  script: php artisan xray:audit
  allow_failure: false

How It Works

Xray is built on nikic/php-parser and performs multi-pass analysis:

  1. Schema loading — Reads your live database schema (tables + columns) and scans app/Models to build a model-to-table map with relationship metadata
  2. AST parsing — Parses each PHP file into an Abstract Syntax Tree
  3. Context detection — Identifies model imports, class declarations, @mixin annotations
  4. Column extraction — Finds all method calls that accept column arguments
  5. Table resolution — Traces each column reference back to its database table using four strategies:
    • Chain resolution: Walk method chains backward (User::where()->orderBy())
    • Closure resolution: Analyze closure parent context (whereHas, where(fn), with)
    • Variable resolution: Track variable assignments and type hints
    • Cross-resolver mediation: Coordinate between strategies for complex patterns
  6. Validation — Check each column against the resolved table's actual schema

Requirements

  • PHP 8.1+
  • Laravel 10, 11, or 12
  • A running database connection (Xray reads schema metadata via Schema::getColumnListing())

License

MIT License. See LICENSE for details.

ikabalzam/laravel-xray 适用场景与选型建议

ikabalzam/laravel-xray 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 230 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 ikabalzam/laravel-xray 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 230
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-08