openeuropa / epoetry-client

PHP client for the ePoetry service.
European Union Public License 1.2
2 stars 2 forks source link

Request for comment - About `TicketValidationInterface` #64

Open drupol opened 1 year ago

drupol commented 1 year ago

Hello colleagues,

I've been reviewing on my own the integration of EU Login from PR #58 and would like to share some observations and suggestions. I would also appreciate your thoughts on these points:

  1. The custom abstraction for ticket validation is an excellent approach, as it promotes interoperability and allows users to implement their own logic. This flexibility is a strong asset for this project.
  2. Concerning the TicketValidationInterface, which is intended to be implemented as a service. As such, its constructor should exclude any parameters that are not essential for its construction. This will streamline the service-building process and help maintain a clean interface.
  3. One key aspect that can be improved in TicketValidationInterface is the method signature for validate(RequestInterface $request). Currently, it lacks the string $jobAccount parameter. Adding this parameter directly to the method signature TicketValidationInterface::validate(string $jobAccount, RequestInterface $request);, rather than incorporating it into the constructor, offers several benefits:
    • Flexibility: By including the jobAccount parameter in the validate method, users can implement the interface without having to modify their existing constructors. This flexibility makes it easier for them to adopt the library in various contexts.
    • Immutability: Moving the jobAccount parameter from the constructor to the method signature encourages class immutability. Immutable classes are less prone to errors, as their state cannot be changed after instantiation. This stability is particularly valuable for libraries that are intended to be integrated into other services.
    • Single Responsibility Principle: By separating the jobAccount parameter from the constructor and including it in the validate method, we adhere to the Single Responsibility Principle. This design choice ensures that the constructor focuses solely on building the service, while the validate method takes care of validation logic. This separation of concerns results in a cleaner, more maintainable codebase. Incorporating these changes into the TicketValidationInterface will not only make the interface more user-friendly but also enhance its overall efficiency and maintainability.
  4. Testing of private methods: I usually recommended to not test private methods directly. Instead of testing private methods directly, I believe we should focus on testing the public API and ensuring that it behaves correctly in various scenarios (through Data Providers). If you feel the need to test a private method, it might be an indication that the method's functionality should be extracted into a separate class with its own public API, which can then be tested independently.
    • Encapsulation: Private methods are meant to be hidden within a class and are not part of the class's public API. They are implementation details designed to support public methods in achieving their functionality. Testing private methods directly can break encapsulation, as you would be accessing and testing internal logic that should remain hidden from the outside world.
    • Focus on Behavior: When writing tests, the primary goal is to ensure that the public API behaves as expected. By testing public methods, you are essentially validating that the system's external behavior is correct, regardless of the internal implementation. Directly testing private methods shifts the focus from the overall behavior to the internal implementation, which can lead to fragile tests that break whenever internal logic is changed.
    • Maintainability: Testing private methods can make code maintenance more challenging. When you test private methods directly, any change in the internal implementation may require updating the tests as well. By focusing on testing public methods, you can refactor or modify the internal implementation without affecting the tests, as long as the public API's behavior remains consistent.
    • Flexibility: Avoiding tests for private methods allows us to refactor or change the implementation of a class without worrying about breaking tests. This flexibility is crucial for adapting to potential new requirements, optimizing performance, or fixing bugs.
  5. Throwing exceptions instead of returning false offers several advantages for error handling and code readability. In summary, throwing exceptions instead of returning false can result in clearer, more maintainable, and more robust code. It encourages proper error handling, provides valuable debugging information, and allows for a standardized approach to managing errors in your application.
  6. Reusability: I also believe that we should prioritize using existing, reputable libraries instead of rewriting parts of their own library, particularly when it comes to security. Well-established libraries have been extensively tested and refined by a large community of developers, ensuring that they are both reliable and efficient. By leveraging these proven solutions, developers can avoid introducing potential vulnerabilities that can arise from creating custom implementations. Furthermore, established libraries often receive regular updates to address any newly discovered security flaws or to keep pace with evolving industry standards. This ensures that the software remains protected against emerging threats. Relying on tried-and-tested libraries not only saves development time and resources, but also helps maintain a more secure and stable software environment. In this context, we could use existing libraries to validate the request.

Shortly after finishing writing this too long comment, I will create and link a pull request that encompasses the following enhancements:

Please share your thoughts, as I recognize that you have already invested a significant amount of time and effort into this project.

Thanks!