- How do I integrate phpcpd into my Laravel project’s CI/CD pipeline?
- Use GitHub Actions, GitLab CI, or CircleCI to run `phpcpd` as a dedicated job. For example, in GitHub Actions, add a step with `php phpcpd.phar --min-tokens=100 --exclude=vendor/`, then configure thresholds to block PRs if duplicates exceed a set percentage. Ensure the PHAR is cached or installed globally for faster execution.
- Can phpcpd work with Laravel’s Artisan commands?
- Yes, wrap `phpcpd.phar` in a custom Artisan command (e.g., `php artisan phpcpd:check`) to run scans on demand. Use the `--report=json` flag to parse results programmatically, or output to HTML for developer dashboards. Exclude Laravel’s `vendor/` and `bootstrap/cache/` directories to avoid false positives.
- What PHP version and Laravel compatibility does phpcpd support?
- phpcpd requires PHP 8.1+ (matching Laravel’s LTS support). Test it with your Laravel version by running `php phpcpd.phar --version` first. For Laravel 10+, ensure no conflicts with Symfony’s Console component, which phpcpd depends on. Exclude generated files like `bootstrap/cache/` to avoid errors.
- How do I configure duplicate detection thresholds to avoid false positives?
- Start with `--min-tokens=50` (adjust based on your codebase) to ignore trivial duplicates. Use `--fuzzy` for semantic similarity checks, but balance it with `--fuzzy-limit` to reduce noise. Test thresholds locally on a subset of files before enforcing them in CI. Laravel’s test suite is a good starting point for validation.
- Is phpcpd safe to run in production or should it only be used in CI?
- phpcpd is a static analysis tool and poses no runtime risk, so it’s safe to run in CI or staging. Avoid running it in production unless debugging a specific issue. Use `--exclude=app/` or similar to limit scans to non-critical paths. For CI, cache the PHAR to avoid redundant downloads.
- How can I exclude specific files or directories from phpcpd scans?
- Use the `--exclude` flag to skip directories like `vendor/`, `bootstrap/cache/`, or test files. For example: `phpcpd.phar --exclude=vendor/ --exclude=tests/Unit/`. To exclude files matching a pattern, use `--exclude=*.php` with a custom script or combine with `find` in CI. Laravel’s `config/` and `database/` directories often don’t need scanning.
- What are the alternatives to phpcpd for Laravel projects?
- Consider **Simian** (Java-based, cross-language) for large projects or **PMD Copy/Paste Detector** (Java) for advanced rule sets. For PHP-only, **PHPMD** (PHP Mess Detector) includes CPD features. If you prefer Composer-based tools, **roave/security-advisories` or `phpstan/phpstan` (with custom rules) can complement phpcpd. Evaluate based on ease of CI integration and report formats.
- How do I handle false positives in phpcpd reports for Laravel?
- False positives often occur in boilerplate code (e.g., Eloquent model stubs) or generated files. Use `--exclude` to whitelist problematic paths, or adjust `--min-tokens` upward. For Laravel, exclude `app/Models/` if duplicates are expected in base models. Document exclusions in your team’s coding standards to maintain consistency.
- Can phpcpd detect duplicates across multiple Laravel modules or packages?
- Yes, phpcpd scans entire directories recursively. For monorepos or multi-package Laravel projects, run it on the root directory: `phpcpd.phar --fuzzy ./`. To optimize performance, split scans by module in CI (e.g., `phpcpd.phar --exclude=*/packages/* ./app`). Use `--report=json` to aggregate results across runs.
- How do I visualize phpcpd results in my Laravel project?
- Generate an HTML report with `--report=html` and serve it via Laravel’s `public/` directory or a dashboard like **Laravel Nova**. For IDE integration, use VSCode’s **PHP-CPD** extension to highlight duplicates inline. Parse JSON reports with Laravel’s `collect()` to build custom dashboards or Slack alerts for critical violations.