Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind SQL invokable object to the provider #30

Merged
merged 23 commits into from
Jun 10, 2024
Merged

Bind SQL invokable object to the provider #30

merged 23 commits into from
Jun 10, 2024

Conversation

koriym
Copy link
Member

@koriym koriym commented Mar 8, 2022

Closes #29

Summary by CodeRabbit

  • New Features

    • Introduced new classes and interfaces for handling SQL queries and exceptions.
    • Added new dependencies for enhanced functionality.
  • Improvements

    • Updated PHP version and dependencies for better compatibility.
    • Enhanced SQL query handling with new public properties and methods.
    • Improved dependency injection configuration for SQL-related classes.
  • Tests

    • Added new test cases for SQL query modules and providers.
    • Improved assertions and array checks in existing test methods.
  • Chores

    • Updated CI and static analysis workflow configurations.

@koriym
Copy link
Member Author

koriym commented Jun 7, 2024

@coderabbitai reviw

Copy link
Contributor

coderabbitai bot commented Jun 7, 2024

@koriym, I will review the changes in the pull request.

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

coderabbitai bot commented Jun 7, 2024

Walkthrough

The updates primarily focus on enhancing SQL query management within a PHP project. Key changes include replacing scheduled triggers in CI workflows, updating PHP version configurations, adding new dependencies, and introducing several new classes and interfaces for SQL query handling. These modifications streamline SQL injection using annotations, improve test coverage, and ensure compatibility with recent PHP versions.

Changes

File(s) Change Summary
.github/workflows/continuous-integration.yml Replaced scheduled trigger with new CI configuration using ray-di workflow.
.github/workflows/static-analysis.yml Updated PHP version from 8.0 to 8.3.
composer.json Added new dependencies koriym/param-reader and nikic/php-parser, updated phpunit/phpunit version.
src/Annotation/Sql.php Added Sql class with a public property $sql and a constructor.
src/Exception/... Introduced SqlFileNotFoundException and SqlNotAnnotatedException extending LogicException.
src/InvokeInterface.php Introduced InvokeInterface extending QueryInterface with __invoke method.
src/RowInterfaceProvider.php Introduced RowInterfaceProvider class implementing ProviderInterface with get() method.
src/RowListInterface.php Modified RowListInterface to extend InvokeInterface instead of QueryInterface.
src/SqlDir.php Introduced SqlDir class with a public property $value and a constructor.
src/SqlFinder.php Introduced SqlFinder class with methods for finding SQL queries and handling exceptions.
src/SqlQueryModule.php Added binding in configure method to bind SqlDir::class to SqlDir instance.
src/SqlQueryProviderModule.php Introduced SqlQueryProviderModule class extending AbstractModule with various bindings.
src/SqlQueryRow.php Modified __invoke method to accept an array of queries.
src/SqlQueryRowList.php Adjusted $lastQuery assignment to remove line break after ternary operator.
src/WebQuery.php Added return type declaration iterable<mixed> to __invoke method.
src/WebQueryModule.php Updated configure() method to include return type void, adjusted array definitions.
tests/Fake/... Introduced several new fake classes for testing SQL query handling and annotations.
tests/Iso8601FormatModuleTest.php Added call to parent constructor in a class.
tests/Iso8601InterceptorTest.php Added assertions for array checks and key existence in test methods.
tests/SqlQueryModuleTest.php Removed line breaks in PHPDoc comment for a test method.
tests/SqlQueryProviderModuleTest.php Introduced test cases for SQL query provider module.
tests/SqlQueryTest.php Added function imports and assertions to check for keys in result array.
tests/WebQueryModuleTest.php Added function imports and assertions for array result and key existence.
vendor-bin/tools/composer.json Added "composer/package-versions-deprecated": true to allow-plugins configuration.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant TodoRepository
    participant SqlFinder
    participant Database

    Client->>TodoRepository: Create Todo
    TodoRepository->>SqlFinder: Find SQL (todo_insert)
    SqlFinder->>Database: Execute SQL
    Database-->>TodoRepository: Return Result
    TodoRepository-->>Client: Return Confirmation
Loading

Assessment against linked issues

Objective Addressed Explanation
SQL specified by Sql annotation/attribute (#29)

Poem

In the land of code where queries reside,
Annotations bloom, no need to hide.
With PHP's might and version new,
SQL finds its path, tried and true.
A rabbit's leap, a coder's delight,
In this update, all feels right. 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range and nitpick comments (1)
src/SqlQueryModule.php (1)

42-42: Binding of SqlDir to an instance in configure method.

Consider documenting the use of the deprecated AliasQuery class to clarify its necessity and planned replacement.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bf190fa and 120ae19.

Files ignored due to path filters (1)
  • vendor-bin/tools/composer.lock is excluded by !**/*.lock
Files selected for processing (21)
  • .github/workflows/continuous-integration.yml (1 hunks)
  • .github/workflows/static-analysis.yml (2 hunks)
  • composer.json (1 hunks)
  • src/Annotation/Sql.php (1 hunks)
  • src/Exception/SqlFileNotFoundException.php (1 hunks)
  • src/Exception/SqlNotAnnotatedException.php (1 hunks)
  • src/InvokeInterface.php (1 hunks)
  • src/RowInterfaceProvider.php (1 hunks)
  • src/RowListInterface.php (1 hunks)
  • src/RowListInterfaceProvider.php (1 hunks)
  • src/SqlDir.php (1 hunks)
  • src/SqlFinder.php (1 hunks)
  • src/SqlQueryModule.php (1 hunks)
  • src/SqlQueryProviderModule.php (1 hunks)
  • src/SqlQueryRowList.php (1 hunks)
  • tests/Fake/FakeTodoProviderSqlNotAnnotated.php (1 hunks)
  • tests/Fake/FakeTodoProviderSqlNotFound.php (1 hunks)
  • tests/Fake/FakeTodoRepository.php (1 hunks)
  • tests/Fake/FakeTodoRepositoryAttr.php (1 hunks)
  • tests/SqlQueryProviderModuleTest.php (1 hunks)
  • vendor-bin/tools/composer.json (1 hunks)
Files skipped from review due to trivial changes (6)
  • .github/workflows/continuous-integration.yml
  • .github/workflows/static-analysis.yml
  • src/Exception/SqlFileNotFoundException.php
  • src/Exception/SqlNotAnnotatedException.php
  • src/SqlDir.php
  • vendor-bin/tools/composer.json
Additional comments not posted (18)
src/RowListInterface.php (1)

7-7: Interface extension to InvokeInterface aligns well with the PR's objectives.

tests/Fake/FakeTodoProviderSqlNotAnnotated.php (1)

7-15: The test class is well-implemented and aligns with the changes in the main codebase.

src/InvokeInterface.php (1)

7-15: The extension of QueryInterface and the implementation of __invoke method are well-documented and align with the PR's objectives.

tests/Fake/FakeTodoProviderSqlNotFound.php (1)

7-20: The use of an intentionally invalid SQL annotation for testing error handling is a good practice.

tests/Fake/FakeTodoRepositoryAttr.php (1)

11-15: Constructor implementation and attribute usage are correct and follow PHP 8 best practices.

src/Annotation/Sql.php (1)

10-24: Annotation setup and constructor implementation are correctly defined and follow best practices for interoperability with Doctrine's system.

tests/Fake/FakeTodoRepository.php (1)

11-34: Use of annotations and constructor setup are correctly implemented for SQL injection and dependency injection in a test environment.

src/RowInterfaceProvider.php (1)

22-35: Constructor and get method implementations are robust, correctly handling dependency injection and dynamic SQL resolution.

src/RowListInterfaceProvider.php (2)

22-30: Constructor correctly initializes class properties with injected dependencies.


32-35: Method get correctly provides an instance of SqlQueryRowList using dynamic SQL retrieval.

src/SqlFinder.php (1)

28-34: Constructor correctly initializes class properties with injected dependencies.

src/SqlQueryRowList.php (1)

57-57: Verify the necessity of using strtolower on the query string, as it might lead to unexpected behavior.

composer.json (2)

20-20: Dependency koriym/param-reader added.

Verification successful

The koriym/param-reader dependency is integrated and used in the following files:

  • src/SqlFinder.php
  • src/SqlQueryProviderModule.php

The usage of ParamReader and ParamReaderInterface in these files confirms the integration of the dependency.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the integration of `koriym/param-reader` in the codebase.

# Test: Search for usage of `koriym/param-reader` classes. Expect: At least one occurrence.
rg --type php 'ParamReader'

Length of output: 548


22-22: Dependency nikic/php-parser added.

tests/SqlQueryProviderModuleTest.php (4)

49-54: Test method testProviderInject looks good.


59-64: Test method testProviderInjectAttr effectively tests attribute-based SQL annotations.


77-82: Test method testSqlFileNotFoundException effectively handles SQL file not found scenarios.


84-89: Test method testSqlNotAnnotated effectively handles scenarios where SQL is not annotated.

src/SqlQueryProviderModule.php Outdated Show resolved Hide resolved
src/SqlFinder.php Show resolved Hide resolved
koriym added 11 commits June 7, 2024 18:55
Changed the binding scope of RowInterface, RowListInterface, and InvokeInterface classes in SqlQueryProviderModule to "Singleton". This update ensures that these classes will have only one instance throughout the application, enhancing performance and reducing resource usage.
The workflows for coding standards, continuous integration, and static analysis have been consolidated and refactored to use external workflow templates managed by Ray-di. This results in sleeker configuration files and ensures uniformity across different projects.
Removed unnecessary typecasting in SqlFinder file. Now, the $param variable is directly used in the sprintf function, improving the efficiency of the code.
The "use" statement for the InvalidArgumentException was added, allowing for a direct name reference instead of a full path, increasing code readability. The code was also refactored to use array destructuring syntax and to remove unnecessary quotes inside method calls which provides less redundancy and improves code consistency.
The phpstan version in the composer.json file has been updated from "^0.12" to "^1.11". This upgrade provides newer features and fixes for static analysis of the code.
This commit involves the removal of the checkMissingIterableValueType option from the phpstan.neon file. This option was preventing the analysis of missing iterable value types, but it has been deemed no longer necessary.
This commit involves the removal of the checkMissingIterableValueType option from the phpstan.neon file. This option was preventing the analysis of missing iterable value types, but it has been deemed no longer necessary.
This commit includes updating the Doctrine coding standard requirement from version 8.2 to 11.1 in composer.json. This change is part of the regular dependency update process to keep the code up-to-date and maintain system compatibility.
The scrutinizer configuration has been updated to support the latest PHP version of 8.2. Specifically, the build environment for PHP was adjusted and some unused tools were removed for simplicity.
The copyright year in the MIT License file has been updated to extend until 2024, reflecting the current duration of the copyright held by Akihito Koriyama.
An annotation has been added to suppress warnings generated by PHPMD's BooleanArgumentFlag rule for the constructor method in the "Query" class. This indicates that the method's design requires a boolean parameter, and false positives on this rule should be ignored.
@koriym koriym force-pushed the provider branch 2 times, most recently from 7c9bc6b to 2f557f6 Compare June 7, 2024 17:30
koriym added 4 commits June 8, 2024 02:39
The readonly attributes were removed from the FakeTodoRepositoryAttr class signature. This change ensures greater flexibility and adaptability of the class members.
The PHPUnit version specified under the "require-dev" section of the composer.json file was increased from "^9.5" to "^9.6.19". This ensures that the project uses the updated PHPUnit version during development.
A small typographical error in the @SuppressWarnings annotation in the Query class has been corrected. The corrected version now properly references the PHPMD (PHP Mess Detector) standard for suppressing boolean argument flag inspections.
@koriym
Copy link
Member Author

koriym commented Jun 7, 2024

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jun 7, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@koriym
Copy link
Member Author

koriym commented Jun 7, 2024

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jun 7, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
src/WebQueryModule.php (1)

Line range hint 56-86: Consider refactoring to reduce duplication in binding logic.

private function configureWebQuery(string $name, string $method, string $uri): void {
    $prefixedName = 'wq-' . $name;
    $interfaces = [QueryInterface::class, RowInterface::class, RowListInterface::class];
    foreach ($interfaces as $interface) {
        $this
            ->bind($interface)
            ->annotatedWith($name)
            ->toConstructor(
                WebQuery::class,
                [
                    'method' => $prefixedName . '-method',
                    'uri' => $prefixedName . '-uri',
                ],
            );
    }
    $this->bind()->annotatedWith($prefixedName . '-method')->toInstance($method);
    $this->bind()->annotatedWith($prefixedName . '-uri')->toInstance($uri);
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 120ae19 and 8911b1a.

Files selected for processing (30)
  • .github/workflows/coding-standards.yml (1 hunks)
  • .github/workflows/continuous-integration.yml (1 hunks)
  • .github/workflows/static-analysis.yml (1 hunks)
  • .scrutinizer.yml (1 hunks)
  • LICENSE (1 hunks)
  • composer.json (1 hunks)
  • phpstan.neon (1 hunks)
  • src-deprecated/SqlAliasInterceptor.php (3 hunks)
  • src/Annotation/Query.php (1 hunks)
  • src/Iso8601FormatModule.php (1 hunks)
  • src/Iso8601Interceptor.php (1 hunks)
  • src/PhpQueryModule.php (2 hunks)
  • src/QueryInterceptor.php (3 hunks)
  • src/RowInterfaceProvider.php (1 hunks)
  • src/RowListInterfaceProvider.php (1 hunks)
  • src/SqlFinder.php (1 hunks)
  • src/SqlQueryModule.php (2 hunks)
  • src/SqlQueryProviderModule.php (1 hunks)
  • src/SqlQueryRow.php (1 hunks)
  • src/SqlQueryRowList.php (2 hunks)
  • src/WebQuery.php (1 hunks)
  • src/WebQueryModule.php (6 hunks)
  • tests/Fake/FakeTodoRepositoryAttr.php (1 hunks)
  • tests/Iso8601FormatModuleTest.php (1 hunks)
  • tests/Iso8601InterceptorTest.php (3 hunks)
  • tests/SqlQueryModuleTest.php (1 hunks)
  • tests/SqlQueryProviderModuleTest.php (1 hunks)
  • tests/SqlQueryTest.php (3 hunks)
  • tests/WebQueryModuleTest.php (2 hunks)
  • vendor-bin/tools/composer.json (1 hunks)
Files skipped from review due to trivial changes (7)
  • LICENSE
  • phpstan.neon
  • src/Annotation/Query.php
  • src/Iso8601Interceptor.php
  • src/QueryInterceptor.php
  • tests/Iso8601FormatModuleTest.php
  • tests/SqlQueryModuleTest.php
Files skipped from review as they are similar to previous changes (10)
  • .github/workflows/static-analysis.yml
  • composer.json
  • src/RowInterfaceProvider.php
  • src/SqlFinder.php
  • src/SqlQueryModule.php
  • src/SqlQueryProviderModule.php
  • src/SqlQueryRowList.php
  • tests/Fake/FakeTodoRepositoryAttr.php
  • tests/SqlQueryProviderModuleTest.php
  • vendor-bin/tools/composer.json
Additional comments not posted (21)
.github/workflows/coding-standards.yml (1)

9-12: Updated workflow to use shared coding standards workflow and specified PHP 8.3.

.github/workflows/continuous-integration.yml (1)

9-14: Updated CI workflow to use shared CI workflow, specified PHP version range, set PHP 8.3 as current stable, and defined a specific script to run.

src/SqlQueryRow.php (1)

28-28: Updated __invoke method to handle multiple queries and ensure the result is iterable.

src/Iso8601FormatModule.php (2)

14-18: Constructor now accepts an array of datetime columns. This change aligns with the PR's objective to handle date-time configurations.


24-31: The configure method correctly binds datetime columns and sets up interceptors. This setup ensures that date-time values are handled appropriately across SQL queries.

src/RowListInterfaceProvider.php (2)

23-31: Constructor correctly initializes dependencies. This standard dependency injection pattern is well-implemented here.


33-36: The get method correctly provides an instance of SqlQueryRowList, ensuring proper dependency handling.

src/PhpQueryModule.php (2)

18-22: Constructor now accepts an iterable of configurations. This change supports dynamic configuration of query bindings.


35-35: The bindQuery method correctly checks for class existence and interface implementation before binding. This ensures that only valid query handlers are bound.

src/WebQuery.php (1)

35-36: The __invoke method's documentation has been updated to specify the return type as iterable<mixed>. This enhances type clarity and aligns with modern PHP practices.

tests/WebQueryModuleTest.php (2)

34-35: Assertions correctly validate the structure and content of the result.


11-12: Use of assert and is_array functions are appropriate for the intended test validations.

tests/Iso8601InterceptorTest.php (2)

29-31: Assertions correctly validate the structure and content of the result, ensuring the date-time format is ISO8601.


55-55: Use of assert function is appropriate for the intended test validations.

tests/SqlQueryTest.php (3)

37-39: Assertions correctly validate the structure and content of the result, ensuring the SQL query execution is as expected.


11-13: Use of assert and is_array functions are appropriate for the intended test validations.


61-65: Assertions correctly validate the structure and content of the result, ensuring the multiple SQL query execution is as expected.

src-deprecated/SqlAliasInterceptor.php (1)

37-37: The method correctly handles the templating of SQL queries and appropriately throws an exception if the URL parsing fails.

Also applies to: 80-80

src/WebQueryModule.php (3)

29-29: Constructor correctly initializes properties and calls the parent constructor.


36-36: Function configure correctly sets up the client and web queries based on the configuration.


101-101: Function configureClient correctly sets up the Guzzle client with appropriate configuration and scope.

@koriym koriym requested a review from coderabbitai[bot] June 7, 2024 20:00
Copy link

@jingu jingu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確認しました。問題ないと思います。
確かにこれなら大量のSQLファイルがあったとしても、開発時のファイル読み込みコストを削減できそうで👍です。

@koriym koriym merged commit 103efbc into 1.x Jun 10, 2024
36 checks passed
@koriym koriym deleted the provider branch June 10, 2024 08:21
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.

SQL specified by Sql annotation/attribute
2 participants