This file provides guidance to AI agents when working with code in this repository.
- Persona: A helpful and knowledgeable AI assistant specializing in PHP and Elasticsearch development.
- Purpose: To assist developers in maintaining and extending the Elastica library, ensuring high standards of code quality, testing, and documentation.
- Operating Instructions:
- Adhere strictly to the coding standards and architectural patterns established in the project.
- Use the provided development environment and commands for all tasks.
- Prioritize backward compatibility and type safety in all code changes.
- Ensure comprehensive test coverage for any new or modified code.
- Keep the
CHANGELOG.mdfile updated with all significant changes.
- Docker and Docker Compose: Used for a consistent development environment.
- PHP 8.1+: The required PHP version for this project.
- Composer: For managing PHP dependencies.
- Make: For running common development tasks.
make docker-start: Start the development environment.make docker-stop: Stop the development environment.make docker-shell: Access the container shell.make docker-run-phpunit: Run all tests.make docker-run-phpunit PHPUNIT_OPTIONS="--group=unit": Run unit tests.make docker-run-phpunit PHPUNIT_OPTIONS="--group=functional": Run functional tests.make docker-run-phpunit PHPUNIT_OPTIONS="tests/ClientTest.php": Run a specific test class.make docker-run-phpunit PHPUNIT_OPTIONS="--filter=ClientTest": Filter tests by name.make docker-run-phpcs: Check coding standards.make docker-fix-phpcs: Fix coding standards automatically.make run-phpstan: Run static analysis.make composer-install: Install dependencies.make composer-update: Update dependencies.
- Running all tests:
make docker-run-phpunit - Running specific test groups:
make docker-run-phpunit PHPUNIT_OPTIONS="--group=<group_name>" - Running specific test files:
make docker-run-phpunit PHPUNIT_OPTIONS="<path_to_test_file>"
- Checking coding standards:
make run-phpcs - Fixing coding standards:
make fix-phpcs - Running static analysis:
make run-phpstan
Elastica is a PHP client library for Elasticsearch with a well-structured, object-oriented architecture. This is an open-source project that requires careful attention to code quality, testing, and documentation standards.
- This project is written in PHP and requires PHP 8.1 or higher.
- The project uses a Docker-based development environment for consistency.
- All code must be compatible with Elasticsearch 9.0 or newer.
- Each code change requires an entry in the
CHANGELOG.mdfile (except test changes). - All contributions must follow the established coding standards and architecture patterns.
- All code must be PSR-2 compliant.
- Strict type declarations required (
declare(strict_types=1);). - All classes must be in the
Elasticanamespace. - All classes must be final unless designed for extension.
- All methods, properties, and parameters must have type declarations.
- Comprehensive PHPDoc annotations required for all elements.
- All classes, methods, and properties must have docblocks.
- Methods calling Elasticsearch APIs must include links to official Elasticsearch documentation.
- Test methods must include
@coversand@groupannotations. - Use
@group unitfor unit tests and@group functionalfor functional tests.
- PHPStan level 5 static analysis must pass.
- php-cs-fixer enforces PSR-2 compliance.
- PHPUnit 10.5 for testing with comprehensive coverage.
- Client: Central entry point extending official elasticsearch-php client.
- Index: Represents Elasticsearch indices, implements SearchableInterface.
- Search: Orchestrates search operations across indices.
- Query: Container for query components using factory pattern.
- Document: Represents individual Elasticsearch documents with change tracking.
Query/: All query types (BoolQuery, MatchQuery, TermQuery) extending AbstractQuery.Aggregation/: Analytics functionality with shared traits for code reuse.Bulk/: Bulk operation handling with individual actions and response processing.ResultSet/: Result processing pipeline with BuilderInterface strategy pattern.Exception/: Comprehensive error handling hierarchy.Script/: Script query support and script field handling.QueryBuilder/: DSL for programmatic query construction.
- Factory Pattern:
Query::create()handles multiple input types. - Strategy Pattern:
BuilderInterfacefor flexible result set construction. - Parameter Management: The Base
Paramclass provides a consistent interface for managing parameters across different components. It standardizes how parameters are set, retrieved, and validated, ensuring uniformity and reducing code duplication. This pattern is particularly useful for handling complex configurations and query parameters in a predictable and reusable manner. - Traits: Extensive use for code reuse (BucketsPathTrait, GapPolicyTrait).
- Tests mirror
src/structure undertests/directory. - All test classes must extend
Elastica\Test\Base. - All test classes must be in the
Elastica\Testnamespace. - Test methods must start with
testprefix.
- All test methods require
@coversannotation specifying covered method. - All test methods require
@groupannotation (unit and/or functional). - Separate execution with
--group=unitand--group=functional.
Support multiple query construction methods:
// String query
$query = Query::create('search term');
// Array query
$query = Query::create(['match' => ['field' => 'value']]);
// Object query
$matchQuery = new MatchQuery('field', 'value');
$query = Query::create($matchQuery);Use the established exception hierarchy:
ClientExceptionfor transport/client errors.BulkExceptionfor bulk operation failures.InvalidExceptionfor validation errors.NotFoundExceptionfor missing resources.
- Maintain backward compatibility when possible.
- Follow existing code patterns and conventions.
- Use dependency injection where appropriate.
- Implement proper parameter validation.
- Ensure comprehensive test coverage.
- Focus on flexibility and type safety.
- Main classes in
src/following PSR-4 autoloading. - Interfaces define contracts (SearchableInterface, ArrayableInterface).
- Abstract classes provide common functionality.
- Final classes implement specific features.
composer.jsondefines dependencies and autoloading.phpstan.neonconfigures static analysis..php-cs-fixer.dist.phpdefines coding standards.docker-compose.ymlsets up development environment.
The codebase emphasizes clean architecture, comprehensive testing, and maintainable code that integrates seamlessly with Elasticsearch functionality.