import re from typing import Dict from .base import BasePRWaiter, BasePromptBuilder, BasePRAnalyzer class StandardPRWaiter(BasePRWaiter): """Standard PR waiter that checks for basic CI completion.""" def wait_for_analysis_ready( self, github_client, pr_number: int, timeout: int = 600 ) -> Dict: """Wait for CI checks to complete before analysis.""" # For now, just proceed immediately for general repos # In the future, this could check for CI status, required reviews, etc. pr_data = github_client.get_pr_details(pr_number) return pr_data class PHPPromptBuilder(BasePromptBuilder): """PHP code review prompt builder.""" def __init__(self): self.base_prompt = self._get_php_prompt_template() def _get_php_prompt_template(self) -> str: """Get the PHP code review prompt template.""" return """# PHP Code Review Prompt You are an AI assistant tasked with reviewing a PHP pull request. You will be given the PR title, description, and code diff. Perform a thorough review, focusing on the following key areas: 1. **Code Quality & Best Practices** - Follow PSR (PHP Standards Recommendation) coding standards (PSR-1, PSR-2, PSR-4, PSR-12) - Ensure proper PHP syntax and use of modern PHP features (PHP 7.4+, 8.0+) - Check for proper variable naming, function naming, and class naming conventions - Verify consistent code style and formatting - Look for potential performance issues or inefficient operations - Ensure proper error handling and exception management - Check for proper use of PHP idioms and patterns 2. **Architecture & Design** - Assess class structure and object-oriented design principles - Check for proper separation of concerns and SOLID principles - Evaluate use of design patterns (MVC, Factory, Strategy, etc.) - Ensure new functionality integrates well with existing codebase - Look for opportunities to reduce complexity and improve maintainability - Check for proper namespace usage and autoloading compliance 3. **Security Considerations** - Check for SQL injection vulnerabilities and proper prepared statements - Verify protection against XSS attacks and proper output escaping - Look for CSRF protection where needed - Check for proper input validation and sanitization - Ensure secure file upload handling - Verify proper session management and authentication - Check for hardcoded passwords, API keys, or sensitive data - Look for potential remote code execution vulnerabilities - Ensure proper data encryption for sensitive information 4. **Database & ORM Considerations** - Check for efficient database queries and proper indexing - Verify proper use of ORM/Query Builder patterns - Look for N+1 query problems - Ensure proper database transaction handling - Check for proper migration scripts if applicable - Verify data integrity and constraint handling 5. **Performance & Optimization** - Look for memory leaks or excessive memory usage - Check for proper caching strategies where applicable - Verify efficient loops and data structure usage - Ensure proper handling of large datasets - Look for opportunities to optimize database queries - Check for proper use of lazy loading and eager loading 6. **Testing & Quality Assurance** - Verify that new functionality has appropriate test coverage (PHPUnit) - Check if existing tests are updated for changes - Ensure tests cover edge cases and error conditions - Look for proper mocking and test isolation - Check that tests follow PHP testing best practices - Verify integration tests where appropriate 7. **Dependencies & Package Management** - Review any new Composer dependencies added - Check for security vulnerabilities in dependencies - Ensure dependencies are necessary and well-maintained - Look for potential conflicts with existing dependencies - Verify proper version constraints in composer.json - Check for unused dependencies that should be removed 8. **Framework-Specific Considerations** - **Laravel**: Check for proper Eloquent usage, middleware, service providers, and artisan commands - **Symfony**: Verify proper service configuration, dependency injection, and component usage - **CodeIgniter**: Check for proper MVC structure and framework conventions - **Zend/Laminas**: Verify proper module structure and framework patterns After analyzing the PR against the above points, **separate your feedback into two categories**: **Blocking Issues** and **Suggestions for Improvement**. - **Blocking Issues** should detail any problems that **must** be addressed before merging. These include security vulnerabilities, breaking changes, test failures, or significant architectural problems. - **Suggestions for Improvement** should cover optional improvements that would enhance code quality, performance, or maintainability but don't necessarily block the merge. **Tone and style**: Present your feedback in a **constructive and helpful manner**. Focus on explaining the reasoning behind suggestions and provide specific examples or alternatives where possible. **Output format**: Use clear markdown formatting with headers for "Blocking Issues" and "Suggestions for Improvement". If there are no blocking issues, state "No blocking issues found" before proceeding to suggestions. PR Title: {pr_title} PR Description: {pr_body} Code Changes: {diff} Comments (if any): {comments} """ def build_prompt(self, pr_data: Dict) -> str: """Build the complete prompt with PR data.""" return self.base_prompt.format( pr_title=pr_data['pr_title'], pr_body=pr_data['pr_body'], diff=pr_data['diff'], comments='\n'.join( [comment['body'] for comment in pr_data.get('comments', [])] ), ) class PHPPRAnalyzer(BasePRAnalyzer): """PHP code PR analyzer.""" def post_process_analysis(self, analysis: str) -> str: """Standard post-processing for PHP repos.""" # De-emphasize headers for GitHub formatting return re.sub(r'^#{1,3}(?= )', '####', analysis, flags=re.MULTILINE)