Skip to content

Conversation

@LaurenceRawlings
Copy link

Add File and Directory Specification Support

Overview

This PR adds the ability to specify individual files and directories for type coverage analysis. Previously, the plugin could only analyse files defined in phpunit.xml sources. Now users can target specific files and directories using command-line arguments.

New Features

File/Directory Arguments

  • Specific File Targeting: Analyse individual PHP files
  • Directory Analysis: Recursively scan directories for PHP files
  • Mixed Arguments: Support combination of files and directories in a single command
  • Automatic PHP File Resolution: Filters out non-PHP files automatically
  • Git Integration: Seamless integration with git commands for analysing changed files

Usage Examples

# Run coverage on specific files
./vendor/bin/pest --type-coverage -- app/Models/User.php app/Services/PaymentService.php

# Run coverage on directories
./vendor/bin/pest --type-coverage -- app/Http app/Models

# Mixed files and directories
./vendor/bin/pest --type-coverage -- app/Models/User.php app/Http/

# Git integration (staged files)
./vendor/bin/pest --type-coverage -- $(git diff --name-only --cached | grep '\.php$' | tr '\n' ' ')

# Git integration (modified files)
./vendor/bin/pest --type-coverage -- $(git diff --name-only | grep '\.php$' | tr '\n' ' ')

# Combined with existing options
./vendor/bin/pest --type-coverage --min=90.0 --compact -- app/Models/

Benefits

  • Developer Productivity: Target specific files for faster feedback loops
  • CI/CD Integration: Analyse only changed files in pull requests
  • Git Workflow: Seamless integration with git commands

Implementation Details

New Components

FileResolver.php - New utility class that:

  • Resolves mixed file/directory paths to PHP files
  • Handles recursive directory scanning
  • Filters non-PHP files automatically
  • Removes duplicate file entries
  • Provides error handling

Enhanced Plugin.php:

  • Improved argument parsing to extract file arguments after -- separator
  • Integration with FileResolver for path resolution
  • Maintains backward compatibility with existing functionality

Testing

Test Suite

FileResolver Unit Tests (tests/Support/FileResolver.php):

  • Edge cases: empty paths, non-existent files, non-PHP files
  • Directory handling: nested directories, empty directories
  • Error scenarios: invalid paths

Plugin Integration Tests (added to tests/Plugin.php):

  • File argument extraction and processing
  • Integration with existing command options
  • Error handling and output validation
  • Git workflow integration

Test Fixtures (tests/Fixtures/TestFiles/):

  • SimpleClass.php: Fully typed class (100% coverage)
  • MissingTypes.php: Class with missing types (50% coverage)
  • SubDir/NestedClass.php: Nested class with missing types (25% coverage)
  • readme.txt: Non-PHP file for filtering tests
  • EmptyDir/: Contains no PHP files used for testing scenarios where no PHP files are found

Backward Compatibility

  • 100% Backward Compatible: All existing functionality preserved
  • Default Behavior Unchanged: When no file arguments provided, uses phpunit.xml sources

TODO: Documentation Updates

Update docs here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant