Skip to content

This repository provides a step-by-step guide to setting up PHPStorm for solving LeetCode problems using PHPUnit for testing and Ray for debugging. It includes detailed instructions, configuration files, and example code to help you get started quickly and efficiently.

Notifications You must be signed in to change notification settings

00bx/DoLeetcodeWithRay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode Problems Setup in PHPStorm

This guide will help you set up your PHPStorm environment to solve LeetCode problems using PHPUnit for testing and Ray for debugging.

Prerequisites

  1. PHP installed via Homebrew on your machine.
  2. Composer installed on your machine.
  3. PHPStorm installed on your machine.
  4. Ray installed on your machine.

Step-by-Step Guide

1. Install PHPStorm

2. Install Composer

3. Install PHP via Homebrew

  • Open your terminal.
  • Install PHP via Homebrew:
  • brew install php

4. Create a New PHP Project in PHPStorm

  • Open PHPStorm and create a new project by selecting File > New Project.
  • Name your project LeetCodeProblems and choose a location for the project.

5. Set Up Project Structure

  • Create the following directory structure within the project:
  • LeetCodeProblems/
    ├── problems/
    │   ├── problem_409/
    │   │   ├── Solution.php
    │   │   └── tests/
    │   │       └── TestCases.php
    └── ...

6. Install PHPUnit and Ray

Install PHPUnit

  • Open your terminal and navigate to the root directory of your project.
  • Run the following command to install PHPUnit:
  • composer require --dev phpunit/phpunit

Install Ray

  • Run the following command to install Ray via Composer:
  • composer require spatie/ray

7. Create Configuration Files

Create phpunit.xml

  • In the root directory of your project, create a file named phpunit.xml with the following content:
  • <?xml version="1.0" encoding="UTF-8"?>
    <phpunit bootstrap="vendor/autoload.php" colors="true">
        <testsuites>
            <testsuite name="Project Test Suite">
                <directory>./tests</directory>
            </testsuite>
        </testsuites>
    </phpunit>

Create ray.php

  • In the root directory of your project, create a file named ray.php with the following content:
  • <?php
    

    return [ 'enable' => true, 'host' => 'localhost', 'port' => 23517, ];

8. Write Your Solution and Test Cases

Solution.php

  • Create Solution.php in problems/problem_409/ with the following content:
  • <?php
    

    class Solution { /**

    • @param String $s

    • @return Integer */ public function longestPalindrome($s) { ray('Starting longestPalindrome with input:', $s); // Debug start

        $charCount = array_fill(0, 128, 0); // ASCII range for upper and lower case letters
      
        // Count the frequency of each character
        for ($i = 0; $i < strlen($s); $i++) {
            $charCount[ord($s[$i])]++;
        }
      
        ray('Character counts:', $charCount); // Debug character counts
      
        $length = 0;
        $oddFound = false;
      
        // Calculate the length of the longest palindrome
        for ($i = 0; $i < 128; $i++) {
            if ($charCount[$i] % 2 == 0) {
                $length += $charCount[$i];
            } else {
                $length += $charCount[$i] - 1;
                $oddFound = true;
            }
        }
      
        // If there was any odd count, we can add one center character
        if ($oddFound) {
            $length++;
        }
      
        ray('Final palindrome length:', $length); // Debug final length
      
        return $length;
      

      } }

TestCases.php

  • Create TestCases.php in problems/problem_409/tests/ with the following content:
  • <?php
    

    use PHPUnit\Framework\TestCase; use Spatie\Ray\Ray;

    require_once DIR . '/../Solution.php'; // Adjust the path as needed

    class TestCases extends TestCase { public function testLongestPalindrome() { $solution = new Solution(); ray()->clearAll(); // Clear previous Ray output

        // Example 1
        $input = "abccccdd";
        $expected = 7;
        ray("Test case 1 - Input: $input, Expected: $expected, Got: {$solution->longestPalindrome($input)}");
        $this->assertEquals($expected, $solution->longestPalindrome($input));
    
        // Example 2
        $input = "a";
        $expected = 1;
        ray("Test case 2 - Input: $input, Expected: $expected, Got: {$solution->longestPalindrome($input)}");
        $this->assertEquals($expected, $solution->longestPalindrome($input));
    }
    

    }

9. Configure PHPStorm

  • Configure PHPUnit in PHPStorm:
    • Go to File > Settings > Languages & Frameworks > PHP > Test Frameworks.
    • Click + and select PHPUnit by Composer autoload.
    • Set the path to the phpunit executable: vendor/bin/phpunit.
  • Configure Ray in PHPStorm:
    • Ensure Ray is properly configured in ray.php and that the Ray application is running on your computer.

10. Run and Debug Your Tests

  • Run Tests in PHPStorm:
    • Right-click on the TestCases.php file or the tests directory and select Run 'PHPUnit tests in ...'.
  • View Ray Output:
    • Open the Ray application to see the debug output from your tests.
  • Debug with Ray:
    • Use ray() calls within your code to send debug messages to Ray.

Example Run

  • Run the Tests:
    • Running the tests in PHPStorm will execute the test cases and output results in the test runner window.
  • View Debug Output in Ray:
    • The Ray application will display messages sent from your tests, helping you see what's happening internally.

© 2024 Zemen (00bx). All rights reserved. 🌟 hope ya have a happy coding ^^🚀

About

This repository provides a step-by-step guide to setting up PHPStorm for solving LeetCode problems using PHPUnit for testing and Ray for debugging. It includes detailed instructions, configuration files, and example code to help you get started quickly and efficiently.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages