Skip to content

ImAliSheraz/booking-code-refractoring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

X.

BookingController.php - Code Review

What’s Good

  • Repository Pattern: Keeps business logic in BookingRepository, making the controller cleaner.
  • Basic Error Handling: Methods like resendSMSNotifications include try-catch blocks for error feedback.

Areas for Improvement

  • Input Validation

    • Issue: Missing input validation can allow invalid data to reach the repository.
    • Solution: Use Laravel’s validate() method to enforce input rules and catch errors early.
  • Inconsistent Error Handling

    • Issue: Not all methods handle errors, risking unexpected results or crashes.
    • Solution: Add error handling, including HTTP status codes like 404 for missing data.
  • Mixed Responsibilities

    • Issue: Some methods perform multiple actions, making code harder to maintain.
    • Solution: Break down tasks into single-purpose methods for better readability and easier troubleshooting.
  • Unclear Default Behavior

    • Issue: In the index method, there’s no clear response if conditions aren’t met.
    • Solution: Add a default response to ensure clear feedback.
  • Direct ENV variables impoort

    • Issue: In the index method, there direct .env varible are using which in not good.
    • Solution: Import .env varible in config file and import in controller from there.

Suggested Refactoring

  • Input Validation: Use Laravel’s request validation to simplify checks.
  • Consistent Error Handling: Apply error handling for clear user feedback.
  • Simplify Method Tasks: Refactor multi-task methods into functions to improve readability and reduce complexity.

These changes will enhance the controller readability, and maintainability.

BookingRepository.php - Code Review

What’s Good

  • Business Logic Separation: Keeps data access separate from controllers, improving code structure.
  • Complex Query Handling: Encapsulates complex query logic within the repository, keeping it seprate from other functions.

Areas for Improvement

  • Code Duplication

    • Issue: Repeated query conditions (e.g., date filtering) make the code harder to maintain.
    • Solution: Create helper methods like fetchJobs, categorizeJobs, and emptyResponse to centralize code logic.
  • Single Responsibility Principle (SRP) Violations

    • Issue: Some methods handle multiple tasks, combining data querying and filtering in one place.
    • Solution: Break down tasks into smaller, modular functions to improve readability and maintainability.
  • Hardcoded Strings and Values

    • Issue: Using magic strings (e.g., 'translator_email') across the code is not good.
    • Solution: Define constants or use configuration values for reusable string literals, improving consistency and resilience.
  • Lack of Input Validation and Data Sanitization

    • Issue: Inputs from controllers aren’t validated, which could lead to unexpected errors.
    • Solution: Implement validation in the repository or use it at the controller level.
  • Performance Considerations

    • Issue: Potential inefficiencies due to redundant queries and lack of eager loading, which may cause performance issues like N+1 problem.
    • Solution: Optimize with eager loading, caching, or batching where possible to reduce database calls.

Suggested Refactoring

  • Refactor for Readability: Convert repeated query conditions into helper methods.
  • Enhanced Structure: Use private methods to centralize filtering logic, making code cleaner and more understandable.

These changes will make the repository codebase more efficient, maintainable, and scalable.

Test Cases

tests/tests/Unit/TeHelperTest/testWillExpire()

Description: This test verifies that the willExpireAt method correctly calculates the expiry time when the due time is set to less than or equal to 90 minutes from the current time.

  • Setup: Creates a $dueTime 80 minutes from now. Sets $createdAt to the current time.
  • Execution: Calls TeHelper::willExpireAt with $dueTime and $createdAt as arguments.
  • Assertions: Asserts that the expiry time matches the formatted $dueTime, confirming it is correctly calculated for times within the 90-minute threshold.

tests/tests/Unit/UserRepositoryTest.php/testCreateOrUpdateCreatesNewUser()

This test ensures that the createOrUpdate method successfully creates a new user when provided with a set of valid user data.

  • Setup: Defines $userData, containing attributes for creating a new user.
  • Execution: Calls $this->repository->createOrUpdate with null as the ID to trigger user creation.
  • Assertions: Checks that the returned object is an instance of the User class. Asserts that the database contains a user with the provided email and name, verifying successful creation.

About

Laravel Code Refracting and Unit Tests

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages