From 1e6928c2d396ba0be4bf1ded4ce35e89abfe1898 Mon Sep 17 00:00:00 2001 From: Pablo Miralles Date: Mon, 20 Jul 2026 12:41:04 +0200 Subject: [PATCH 1/4] Add cars-assemble concept exercise for if and comparisons. Unlocks if-control-structures and comparison-operators after booleans and numbers, ported from csharp/cars-assemble. Co-authored-by: Cursor --- config.json | 16 ++ .../concept/cars-assemble/.docs/hints.md | 25 +++ .../cars-assemble/.docs/instructions.md | 58 +++++ .../cars-assemble/.docs/introduction.md | 93 ++++++++ .../cars-assemble/.docs/introduction.md.tpl | 5 + .../concept/cars-assemble/.meta/config.json | 20 ++ .../concept/cars-assemble/.meta/design.md | 40 ++++ .../concept/cars-assemble/.meta/exemplar.php | 37 +++ .../concept/cars-assemble/CarsAssemble.php | 19 ++ .../cars-assemble/CarsAssembleTest.php | 210 ++++++++++++++++++ 10 files changed, 523 insertions(+) create mode 100644 exercises/concept/cars-assemble/.docs/hints.md create mode 100644 exercises/concept/cars-assemble/.docs/instructions.md create mode 100644 exercises/concept/cars-assemble/.docs/introduction.md create mode 100644 exercises/concept/cars-assemble/.docs/introduction.md.tpl create mode 100644 exercises/concept/cars-assemble/.meta/config.json create mode 100644 exercises/concept/cars-assemble/.meta/design.md create mode 100644 exercises/concept/cars-assemble/.meta/exemplar.php create mode 100644 exercises/concept/cars-assemble/CarsAssemble.php create mode 100644 exercises/concept/cars-assemble/CarsAssembleTest.php diff --git a/config.json b/config.json index 670d309ad..4159afde5 100644 --- a/config.json +++ b/config.json @@ -82,6 +82,22 @@ ], "status": "beta" }, + { + "slug": "cars-assemble", + "name": "Cars, Assemble!", + "uuid": "4949e5b9-c685-4380-9d70-46ae970035cf", + "concepts": [ + "comparison-operators", + "if-control-structures" + ], + "prerequisites": [ + "booleans", + "integers", + "floating-point-numbers", + "arithmetic-operators" + ], + "status": "beta" + }, { "slug": "windowing-system", "name": "Windowing System", diff --git a/exercises/concept/cars-assemble/.docs/hints.md b/exercises/concept/cars-assemble/.docs/hints.md new file mode 100644 index 000000000..b85916185 --- /dev/null +++ b/exercises/concept/cars-assemble/.docs/hints.md @@ -0,0 +1,25 @@ +# Hints + +## General + +- Review the [comparison operators][comparison-operators] and [control structures][if-statement] documentation. + +## 1. Calculate the success rate + +- Determining the success rate can be done through a [conditional statement][if-statement]. +- Numbers can be compared using the built-in [comparison operators][comparison-operators]. + +## 2. Calculate the production rate per hour + +- Use the `CarsAssemble.successRate()` method you wrote earlier to determine the success rate. +- PHP allows multiplication between integers and floating-point numbers. + The result will be a floating-point number when either operand is a float. + +## 3. Calculate the number of working items produced per minute + +- Converting a floating-point number to an integer discards the fractional part (truncation toward zero). +- You can cast to an integer with `(int)` or use [`intval()`][intval]. + +[comparison-operators]: https://www.php.net/manual/en/language.operators.comparison.php +[if-statement]: https://www.php.net/manual/en/control-structures.if.php +[intval]: https://www.php.net/manual/en/function.intval.php diff --git a/exercises/concept/cars-assemble/.docs/instructions.md b/exercises/concept/cars-assemble/.docs/instructions.md new file mode 100644 index 000000000..616e004c0 --- /dev/null +++ b/exercises/concept/cars-assemble/.docs/instructions.md @@ -0,0 +1,58 @@ +# Instructions + +In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. +The assembly line's speed can range from `0` (off) to `10` (maximum). + +At its lowest speed (`1`), `221` cars are produced each hour. +The production increases linearly with the speed. +So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. +However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. + +You have three tasks. + +## 1. Calculate the success rate + +Implement the `CarsAssemble.successRate()` method to calculate the ratio of an item being created without error for a given speed. +The following table shows how speed influences the success rate: + +- `0`: 0% success rate. +- `1` to `4`: 100% success rate. +- `5` to `8`: 90% success rate. +- `9`: 80% success rate. +- `10`: 77% success rate. + +```php +successRate(10); +// => 0.77 +``` + +## 2. Calculate the production rate per hour + +Implement the `CarsAssemble.productionRatePerHour()` method to calculate the assembly line's production rate per hour, taking into account its success rate: + +```php +productionRatePerHour(6); +// => 1193.4 +``` + +Note that the value returned is a floating-point number. + +## 3. Calculate the number of working items produced per minute + +Implement the `CarsAssemble.workingItemsPerMinute()` method to calculate how many working cars are produced per minute: + +```php +workingItemsPerMinute(6); +// => 19 +``` + +Note that the value returned is an integer. diff --git a/exercises/concept/cars-assemble/.docs/introduction.md b/exercises/concept/cars-assemble/.docs/introduction.md new file mode 100644 index 000000000..c8712fd8d --- /dev/null +++ b/exercises/concept/cars-assemble/.docs/introduction.md @@ -0,0 +1,93 @@ +# Introduction + +## Comparison Operators + +PHP has ten built in comparison operators: + +| Name | Example | Result | +| ----- | ---------- | -------------------------------------------------- | +| Equal | `$a == $b` | true if `$a` is equal to `$b` after type juggling. | +| Identical | `$a === $b` | true if `$a` is equal to `$b` and the same type. | +| Not Equal | `$a != $b` | true if `$a` is not equal to `$b` after type juggling. | +| Not Equal | `$a <> $b` | true if `$a` is not equal to `$b` after type juggling. | +| Not identical | `$a !== $b` | true if `$a` is not equal to `$b` or not of the same type. | +| Less than | `$a < $b` | true if `$a` is strictly less than `$b`. | +| Greater than | `$a > $b` | true if `$a` is strictly greater than `$b`. | +| Less than or equal to | `$a <= $b` | true if `$a` is less than or equal to `$b`. | +| Greater than or equal to | `$a >= $b` | true if `$a` is greater than or equal to `$b`. | +| Spaceship | `$a <=> $b` | returns an integer less than, equal to, or greater than `0`i when `$a`. | + +PHP has distinct definitions that differentiate between `equal` and `identical`. +Sometimes `identical` is also referred to as `strictly equal`. + +```php + true, equal +1 === "1"; // => false, not identical + +// Comparisons between integers and floating point values +1 == 1.0; // => true +1 === 1.0; // => false + +// Comparisons between object instances +new stdClass() == new stdClass(); // => true, properties are equal +new stdClass() === new stdClass(); // => false, references are not identical +``` + +## If, Else, Elseif + +Conditional statements using `if`, `elseif`, and `else` are a fundamental parts of program control flow. +The `if` statement evaluates an expression, and if `true`, will execute the code branch. + +```php +`, `<=`, `>=`). +- Know the difference between equal (`==`) and identical (`===`) comparisons. +- Know how to conditionally execute code using `if` statements. +- Know how to chain conditions with `elseif` / multiple `if` statements. +- Know how to convert a floating-point number to an integer by truncation. + +## Out of scope + +- `switch` / `match` expressions. +- The ternary operator. +- Type declarations. +- `declare(strict_types=1)`. + +## Concepts + +- `comparison-operators`: know how to compare values; know the difference between equal and identical comparisons. +- `if-control-structures`: know how to conditionally execute code using `if` / `elseif` / `else`. + +## Prerequisites + +- `booleans`: know how to use boolean values and operators. +- `integers`: know how to work with whole numbers. +- `floating-point-numbers`: know how to work with floating-point numbers. +- `arithmetic-operators`: know how to multiply and divide numbers. + +## Analyzer + +This exercise could benefit from the following rules: + +- `actionable`: If the student did not reuse `successRate` inside `productionRatePerHour`, instruct them to do so. +- `informative`: If the solution repeatedly hard-codes `221`, suggest storing it in a constant. +- `informative`: If the solution uses `if`/`elseif` with early returns, note that trailing `else` branches may be redundant. diff --git a/exercises/concept/cars-assemble/.meta/exemplar.php b/exercises/concept/cars-assemble/.meta/exemplar.php new file mode 100644 index 000000000..9bd062f55 --- /dev/null +++ b/exercises/concept/cars-assemble/.meta/exemplar.php @@ -0,0 +1,37 @@ += 5) { + return 0.9; + } + + if ($speed >= 1) { + return 1.0; + } + + return 0.0; + } + + public function productionRatePerHour($speed) + { + return self::CARS_PER_HOUR * $speed * $this->successRate($speed); + } + + public function workingItemsPerMinute($speed) + { + return (int) ($this->productionRatePerHour($speed) / 60); + } +} diff --git a/exercises/concept/cars-assemble/CarsAssemble.php b/exercises/concept/cars-assemble/CarsAssemble.php new file mode 100644 index 000000000..cb5b5c182 --- /dev/null +++ b/exercises/concept/cars-assemble/CarsAssemble.php @@ -0,0 +1,19 @@ +successRate(0); + $this->assertEqualsWithDelta(0.0, $actual, 0.001); + } + + /** + * @task_id 1 + */ + #[TestDox('Success rate for speed 1')] + public function testSuccessRateForSpeedOne() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->successRate(1); + $this->assertEqualsWithDelta(1.0, $actual, 0.001); + } + + /** + * @task_id 1 + */ + #[TestDox('Success rate for speed 4')] + public function testSuccessRateForSpeedFour() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->successRate(4); + $this->assertEqualsWithDelta(1.0, $actual, 0.001); + } + + /** + * @task_id 1 + */ + #[TestDox('Success rate for speed 5')] + public function testSuccessRateForSpeedFive() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->successRate(5); + $this->assertEqualsWithDelta(0.9, $actual, 0.001); + } + + /** + * @task_id 1 + */ + #[TestDox('Success rate for speed 9')] + public function testSuccessRateForSpeedNine() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->successRate(9); + $this->assertEqualsWithDelta(0.8, $actual, 0.001); + } + + /** + * @task_id 1 + */ + #[TestDox('Success rate for speed 10')] + public function testSuccessRateForSpeedTen() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->successRate(10); + $this->assertEqualsWithDelta(0.77, $actual, 0.001); + } + + /** + * @task_id 2 + */ + #[TestDox('Production rate per hour for speed 0')] + public function testProductionRatePerHourForSpeedZero() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->productionRatePerHour(0); + $this->assertEqualsWithDelta(0.0, $actual, 0.1); + } + + /** + * @task_id 2 + */ + #[TestDox('Production rate per hour for speed 1')] + public function testProductionRatePerHourForSpeedOne() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->productionRatePerHour(1); + $this->assertEqualsWithDelta(221.0, $actual, 0.1); + } + + /** + * @task_id 2 + */ + #[TestDox('Production rate per hour for speed 4')] + public function testProductionRatePerHourForSpeedFour() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->productionRatePerHour(4); + $this->assertEqualsWithDelta(884.0, $actual, 0.1); + } + + /** + * @task_id 2 + */ + #[TestDox('Production rate per hour for speed 7')] + public function testProductionRatePerHourForSpeedSeven() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->productionRatePerHour(7); + $this->assertEqualsWithDelta(1392.3, $actual, 0.1); + } + + /** + * @task_id 2 + */ + #[TestDox('Production rate per hour for speed 9')] + public function testProductionRatePerHourForSpeedNine() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->productionRatePerHour(9); + $this->assertEqualsWithDelta(1591.2, $actual, 0.1); + } + + /** + * @task_id 2 + */ + #[TestDox('Production rate per hour for speed 10')] + public function testProductionRatePerHourForSpeedTen() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->productionRatePerHour(10); + $this->assertEqualsWithDelta(1701.7, $actual, 0.1); + } + + /** + * @task_id 3 + */ + #[TestDox('Working items per minute for speed 0')] + public function testWorkingItemsPerMinuteForSpeedZero() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->workingItemsPerMinute(0); + $this->assertSame(0, $actual); + } + + /** + * @task_id 3 + */ + #[TestDox('Working items per minute for speed 1')] + public function testWorkingItemsPerMinuteForSpeedOne() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->workingItemsPerMinute(1); + $this->assertSame(3, $actual); + } + + /** + * @task_id 3 + */ + #[TestDox('Working items per minute for speed 5')] + public function testWorkingItemsPerMinuteForSpeedFive() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->workingItemsPerMinute(5); + $this->assertSame(16, $actual); + } + + /** + * @task_id 3 + */ + #[TestDox('Working items per minute for speed 8')] + public function testWorkingItemsPerMinuteForSpeedEight() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->workingItemsPerMinute(8); + $this->assertSame(26, $actual); + } + + /** + * @task_id 3 + */ + #[TestDox('Working items per minute for speed 9')] + public function testWorkingItemsPerMinuteForSpeedNine() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->workingItemsPerMinute(9); + $this->assertSame(26, $actual); + } + + /** + * @task_id 3 + */ + #[TestDox('Working items per minute for speed 10')] + public function testWorkingItemsPerMinuteForSpeedTen() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->workingItemsPerMinute(10); + $this->assertSame(28, $actual); + } +} From 242b4acd43ef639a5735a0c746ee168b0fa79161 Mon Sep 17 00:00:00 2001 From: Pablo Miralles Date: Sun, 26 Jul 2026 17:35:29 +0200 Subject: [PATCH 2/4] Refine cars-assemble around comparison operators. Focus the exercise on one concept, simplify the exemplar, and align docs with maintainer feedback. Co-authored-by: Cursor --- concepts/comparison-operators/about.md | 79 +++++++++----- concepts/comparison-operators/introduction.md | 75 ++++++++----- config.json | 6 +- .../concept/cars-assemble/.docs/hints.md | 25 +++-- .../cars-assemble/.docs/instructions.md | 60 +++++++---- .../cars-assemble/.docs/introduction.md | 100 ++++++------------ .../cars-assemble/.docs/introduction.md.tpl | 2 - .../concept/cars-assemble/.meta/config.json | 2 +- .../concept/cars-assemble/.meta/design.md | 47 ++++---- .../concept/cars-assemble/.meta/exemplar.php | 15 +-- .../concept/cars-assemble/CarsAssemble.php | 7 +- .../cars-assemble/CarsAssembleTest.php | 77 +++++++------- 12 files changed, 280 insertions(+), 215 deletions(-) diff --git a/concepts/comparison-operators/about.md b/concepts/comparison-operators/about.md index 319df12ba..02521f742 100644 --- a/concepts/comparison-operators/about.md +++ b/concepts/comparison-operators/about.md @@ -1,35 +1,66 @@ # Comparison Operators -PHP has ten built in comparison operators: - -| Name | Example | Result | -| ----- | ---------- | -------------------------------------------------- | -| Equal | `$a == $b` | true if `$a` is equal to `$b` after type juggling. | -| Identical | `$a === $b` | true if `$a` is equal to `$b` and the same type. | -| Not Equal | `$a != $b` | true if `$a` is not equal to `$b` after type juggling. | -| Not Equal | `$a <> $b` | true if `$a` is not equal to `$b` after type juggling. | -| Not identical | `$a !== $b` | true if `$a` is not equal to `$b` or not of the same type. | -| Less than | `$a < $b` | true if `$a` is strictly less than `$b`. | -| Greater than | `$a > $b` | true if `$a` is strictly greater than `$b`. | -| Less than or equal to | `$a <= $b` | true if `$a` is less than or equal to `$b`. | -| Greater than or equal to | `$a >= $b` | true if `$a` is greater than or equal to `$b`. | -| Spaceship | `$a <=> $b` | returns an integer less than, equal to, or greater than `0`i when `$a`. | - -PHP has distinct definitions that differentiate between `equal` and `identical`. -Sometimes `identical` is also referred to as `strictly equal`. +Comparison operators compare two values. +Most of them return a boolean (`true` or `false`). +The spaceship operator (`<=>`) is different: it returns `-1`, `0`, or `1`. + +PHP has ten built-in comparison operators: + +| Name | Example | Result | +| --- | --- | --- | +| Equal | `$a == $b` | `true` if `$a` is equal to `$b` after type juggling | +| Identical | `$a === $b` | `true` if `$a` is equal to `$b` and the same type | +| Not Equal | `$a != $b` | `true` if `$a` is not equal to `$b` after type juggling | +| Not Equal | `$a <> $b` | `true` if `$a` is not equal to `$b` after type juggling | +| Not identical | `$a !== $b` | `true` if `$a` is not equal to `$b` or not the same type | +| Less than | `$a < $b` | `true` if `$a` is strictly less than `$b` | +| Greater than | `$a > $b` | `true` if `$a` is strictly greater than `$b` | +| Less than or equal to | `$a <= $b` | `true` if `$a` is less than or equal to `$b` | +| Greater than or equal to | `$a >= $b` | `true` if `$a` is greater than or equal to `$b` | +| Spaceship | `$a <=> $b` | `-1`, `0`, or `1` when `$a` is less than, equal to, or greater than `$b` | + +## Equal and identical + +PHP distinguishes between **equal** (`==`) and **identical** (`===`). +Identical is also called **strictly equal**. + +With `==`, PHP may change the type of a value before comparing (type juggling). +With `===`, both the value and the type must match. ```php true, equal +1 == "1"; // => true, equal after type juggling 1 === "1"; // => false, not identical -// Comparisons between integers and floating point values -1 == 1.0; // => true +1 == 1.0; // => true 1 === 1.0; // => false +``` + +The same idea applies to "not equal" (`!=` / `<>`) versus "not identical" (`!==`). + +## Comparing objects + +For objects, `==` checks whether properties are equal. +`===` checks whether both sides refer to the same instance. + +```php + true +new stdClass() === new stdClass(); // => false +``` + +## The spaceship operator -// Comparisons between object instances -new stdClass() == new stdClass(); // => true, properties are equal -new stdClass() === new stdClass(); // => false, references are not identical +`$a <=> $b` returns: + +```php + 2; // => -1 +1 <=> 1; // => 0 +2 <=> 1; // => 1 ``` + +This is useful when you need an ordering result, not only `true` or `false`. diff --git a/concepts/comparison-operators/introduction.md b/concepts/comparison-operators/introduction.md index 601e43d0e..b46314694 100644 --- a/concepts/comparison-operators/introduction.md +++ b/concepts/comparison-operators/introduction.md @@ -1,36 +1,59 @@ # Comparison Operators -PHP has ten built in comparison operators: - -| Name | Example | Result | -| ----- | ---------- | -------------------------------------------------- | -| Equal | `$a == $b` | true if `$a` is equal to `$b` after type juggling. | -| Identical | `$a === $b` | true if `$a` is equal to `$b` and the same type. | -| Not Equal | `$a != $b` | true if `$a` is not equal to `$b` after type juggling. | -| Not Equal | `$a <> $b` | true if `$a` is not equal to `$b` after type juggling. | -| Not identical | `$a !== $b` | true if `$a` is not equal to `$b` or not of the same type. | -| Less than | `$a < $b` | true if `$a` is strictly less than `$b`. | -| Greater than | `$a > $b` | true if `$a` is strictly greater than `$b`. | -| Less than or equal to | `$a <= $b` | true if `$a` is less than or equal to `$b`. | -| Greater than or equal to | `$a >= $b` | true if `$a` is greater than or equal to `$b`. | -| Spaceship | `$a <=> $b` | returns an integer less than, equal to, or greater than `0`i when `$a`. | - -PHP has distinct definitions that differentiate between `equal` and `identical`. -Sometimes `identical` is also referred to as `strictly equal`. +Comparison operators compare two values and usually return a boolean (`true` or `false`). +They are commonly used to make decisions in code. + +For learning PHP, start with **identical** comparisons and relational comparisons between numbers: ```php true, equal -1 === "1"; // => false, not identical +5 === 5; // => true +5 !== 0; // => true +3 < 7; // => true +9 > 2; // => true +5 <= 5; // => true +4 >= 8; // => false +``` + +| Operator | Meaning | +| --- | --- | +| `$a === $b` | identical: equal and the same type | +| `$a !== $b` | not identical | +| `$a < $b` | less than | +| `$a > $b` | greater than | +| `$a <= $b` | less than or equal to | +| `$a >= $b` | greater than or equal to | + +## The spaceship operator -// Comparisons between integers and floating point values -1 == 1.0; // => true -1 === 1.0; // => false +The spaceship operator (`<=>`) also compares two values, but it returns an integer instead of a boolean: + +- `-1` when the left value is less than the right value +- `0` when both values are equal +- `1` when the left value is greater than the right value + +```php + true, properties are equal -new stdClass() === new stdClass(); // => false, references are not identical +3 <=> 7; // => -1 +5 <=> 5; // => 0 +9 <=> 2; // => 1 ``` +## Using comparisons in an `if` statement + +A comparison can be used as the condition of an `if` statement. +If the comparison is `true`, the code inside the braces runs: + +```php += 5) { + return 0.9; +} +``` diff --git a/config.json b/config.json index 4159afde5..2468989f0 100644 --- a/config.json +++ b/config.json @@ -87,14 +87,12 @@ "name": "Cars, Assemble!", "uuid": "4949e5b9-c685-4380-9d70-46ae970035cf", "concepts": [ - "comparison-operators", - "if-control-structures" + "comparison-operators" ], "prerequisites": [ "booleans", "integers", - "floating-point-numbers", - "arithmetic-operators" + "floating-point-numbers" ], "status": "beta" }, diff --git a/exercises/concept/cars-assemble/.docs/hints.md b/exercises/concept/cars-assemble/.docs/hints.md index b85916185..bf9343a84 100644 --- a/exercises/concept/cars-assemble/.docs/hints.md +++ b/exercises/concept/cars-assemble/.docs/hints.md @@ -2,24 +2,27 @@ ## General -- Review the [comparison operators][comparison-operators] and [control structures][if-statement] documentation. +- The introduction covers the comparison operators you need for this exercise. +- You may also review the [comparison operators][comparison-operators] documentation. ## 1. Calculate the success rate -- Determining the success rate can be done through a [conditional statement][if-statement]. -- Numbers can be compared using the built-in [comparison operators][comparison-operators]. +- Compare `$speed` with the boundary values from the table (`===`, `>=`, `>`, `<=`, or `<` as needed). +- You can use a simple `if` statement to return the matching success rate. ## 2. Calculate the production rate per hour -- Use the `CarsAssemble.successRate()` method you wrote earlier to determine the success rate. -- PHP allows multiplication between integers and floating-point numbers. - The result will be a floating-point number when either operand is a float. +- Reuse the `successRate()` method you wrote earlier: `$this->successRate($speed)`. +- Multiply `221`, `$speed`, and the success rate. +- PHP can multiply integers and floating-point numbers together; the result may be a float. -## 3. Calculate the number of working items produced per minute +## 3. Check whether the line is running -- Converting a floating-point number to an integer discards the fractional part (truncation toward zero). -- You can cast to an integer with `(int)` or use [`intval()`][intval]. +- Use a not-identical comparison (`!==`) against `0`. +- Return the boolean result of that comparison. + +## 4. Compare two line speeds + +- The spaceship operator (`<=>`) returns `-1`, `0`, or `1` directly. [comparison-operators]: https://www.php.net/manual/en/language.operators.comparison.php -[if-statement]: https://www.php.net/manual/en/control-structures.if.php -[intval]: https://www.php.net/manual/en/function.intval.php diff --git a/exercises/concept/cars-assemble/.docs/instructions.md b/exercises/concept/cars-assemble/.docs/instructions.md index 616e004c0..85032ed6d 100644 --- a/exercises/concept/cars-assemble/.docs/instructions.md +++ b/exercises/concept/cars-assemble/.docs/instructions.md @@ -1,25 +1,25 @@ # Instructions -In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. +In this exercise you'll write code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from `0` (off) to `10` (maximum). At its lowest speed (`1`), `221` cars are produced each hour. The production increases linearly with the speed. -So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. +So with the speed set to `4`, the line should produce `4 * 221 = 884` cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. -You have three tasks. +You have four tasks. ## 1. Calculate the success rate -Implement the `CarsAssemble.successRate()` method to calculate the ratio of an item being created without error for a given speed. -The following table shows how speed influences the success rate: +Implement the `successRate()` method to calculate the ratio of cars created without error for a given speed. +Use comparisons to choose the correct rate from this table: -- `0`: 0% success rate. -- `1` to `4`: 100% success rate. -- `5` to `8`: 90% success rate. -- `9`: 80% success rate. -- `10`: 77% success rate. +- `0`: 0% success rate (`0.0`) +- `1` to `4`: 100% success rate (`1.0`) +- `5` to `8`: 90% success rate (`0.9`) +- `9`: 80% success rate (`0.8`) +- `10`: 77% success rate (`0.77`) ```php successRate(10); ## 2. Calculate the production rate per hour -Implement the `CarsAssemble.productionRatePerHour()` method to calculate the assembly line's production rate per hour, taking into account its success rate: +Implement the `productionRatePerHour()` method to calculate how many cars are successfully produced per hour. +Multiply the base production (`221` cars per hour at speed `1`) by the speed and by the success rate for that speed. ```php productionRatePerHour(6); // => 1193.4 ``` -Note that the value returned is a floating-point number. +## 3. Check whether the line is running -## 3. Calculate the number of working items produced per minute - -Implement the `CarsAssemble.workingItemsPerMinute()` method to calculate how many working cars are produced per minute: +Implement the `isLineRunning()` method to return whether the assembly line is running. +The line is running when its speed is **not identical** to `0`. ```php workingItemsPerMinute(6); -// => 19 +$assembly_line->isLineRunning(0); +// => false + +$assembly_line->isLineRunning(3); +// => true ``` -Note that the value returned is an integer. +## 4. Compare two line speeds + +Implement the `compareSpeeds()` method to compare two speeds. +It should return: + +- `-1` when the first speed is less than the second +- `0` when both speeds are equal +- `1` when the first speed is greater than the second + +```php +compareSpeeds(3, 7); +// => -1 + +$assembly_line->compareSpeeds(5, 5); +// => 0 + +$assembly_line->compareSpeeds(9, 2); +// => 1 +``` diff --git a/exercises/concept/cars-assemble/.docs/introduction.md b/exercises/concept/cars-assemble/.docs/introduction.md index c8712fd8d..5192ccc83 100644 --- a/exercises/concept/cars-assemble/.docs/introduction.md +++ b/exercises/concept/cars-assemble/.docs/introduction.md @@ -2,92 +2,60 @@ ## Comparison Operators -PHP has ten built in comparison operators: - -| Name | Example | Result | -| ----- | ---------- | -------------------------------------------------- | -| Equal | `$a == $b` | true if `$a` is equal to `$b` after type juggling. | -| Identical | `$a === $b` | true if `$a` is equal to `$b` and the same type. | -| Not Equal | `$a != $b` | true if `$a` is not equal to `$b` after type juggling. | -| Not Equal | `$a <> $b` | true if `$a` is not equal to `$b` after type juggling. | -| Not identical | `$a !== $b` | true if `$a` is not equal to `$b` or not of the same type. | -| Less than | `$a < $b` | true if `$a` is strictly less than `$b`. | -| Greater than | `$a > $b` | true if `$a` is strictly greater than `$b`. | -| Less than or equal to | `$a <= $b` | true if `$a` is less than or equal to `$b`. | -| Greater than or equal to | `$a >= $b` | true if `$a` is greater than or equal to `$b`. | -| Spaceship | `$a <=> $b` | returns an integer less than, equal to, or greater than `0`i when `$a`. | - -PHP has distinct definitions that differentiate between `equal` and `identical`. -Sometimes `identical` is also referred to as `strictly equal`. +Comparison operators compare two values and usually return a boolean (`true` or `false`). +They are commonly used to make decisions in code. + +For learning PHP, start with **identical** comparisons and relational comparisons between numbers: ```php true, equal -1 === "1"; // => false, not identical +5 === 5; // => true +5 !== 0; // => true +3 < 7; // => true +9 > 2; // => true +5 <= 5; // => true +4 >= 8; // => false +``` -// Comparisons between integers and floating point values -1 == 1.0; // => true -1 === 1.0; // => false +| Operator | Meaning | +| --- | --- | +| `$a === $b` | identical: equal and the same type | +| `$a !== $b` | not identical | +| `$a < $b` | less than | +| `$a > $b` | greater than | +| `$a <= $b` | less than or equal to | +| `$a >= $b` | greater than or equal to | -// Comparisons between object instances -new stdClass() == new stdClass(); // => true, properties are equal -new stdClass() === new stdClass(); // => false, references are not identical -``` +### The spaceship operator -## If, Else, Elseif +The spaceship operator (`<=>`) also compares two values, but it returns an integer instead of a boolean: -Conditional statements using `if`, `elseif`, and `else` are a fundamental parts of program control flow. -The `if` statement evaluates an expression, and if `true`, will execute the code branch. +- `-1` when the left value is less than the right value +- `0` when both values are equal +- `1` when the left value is greater than the right value ```php 7; // => -1 +5 <=> 5; // => 0 +9 <=> 2; // => 1 ``` -If the expression is not a boolean value, the evaluation determines the value's "truthiness" or "not falsiness". -In PHP, the following values are considered equal to false: - -- boolean `false` -- integer `0` -- float `0.0` and `-0.0` -- an empty array `[]` -- an empty string `""` or a numeric string `"0"` -- `null` - -All other values are considered true. +### Using comparisons in an `if` statement -### Responding to multiple conditions - -Following an `if` statement, you may chain multiple conditions using `elseif` and `else`. +A comparison can be used as the condition of an `if` statement. +If the comparison is `true`, the code inside the braces runs: ```php = 5) { + return 0.9; } ``` diff --git a/exercises/concept/cars-assemble/.docs/introduction.md.tpl b/exercises/concept/cars-assemble/.docs/introduction.md.tpl index 2653f6bfa..1a3e7c8f8 100644 --- a/exercises/concept/cars-assemble/.docs/introduction.md.tpl +++ b/exercises/concept/cars-assemble/.docs/introduction.md.tpl @@ -1,5 +1,3 @@ # Introduction %{concept:comparison-operators} - -%{concept:if-control-structures} diff --git a/exercises/concept/cars-assemble/.meta/config.json b/exercises/concept/cars-assemble/.meta/config.json index 8fa2b0dec..5072dc18b 100644 --- a/exercises/concept/cars-assemble/.meta/config.json +++ b/exercises/concept/cars-assemble/.meta/config.json @@ -16,5 +16,5 @@ "forked_from": [ "csharp/cars-assemble" ], - "blurb": "Learn about comparisons and if statements by analyzing a car assembly line!" + "blurb": "Learn about comparison operators by analyzing a car assembly line!" } diff --git a/exercises/concept/cars-assemble/.meta/design.md b/exercises/concept/cars-assemble/.meta/design.md index 6f1b408df..3c42ccfba 100644 --- a/exercises/concept/cars-assemble/.meta/design.md +++ b/exercises/concept/cars-assemble/.meta/design.md @@ -2,39 +2,46 @@ ## Goal -This exercise teaches students how to use comparison operators and `if` control structures to branch logic based on numeric ranges. +Teach students how to compare numbers with PHP comparison operators, using a small factory production story. ## Learning objectives -- Know how to compare values using comparison operators (`==`, `===`, `<`, `>`, `<=`, `>=`). -- Know the difference between equal (`==`) and identical (`===`) comparisons. -- Know how to conditionally execute code using `if` statements. -- Know how to chain conditions with `elseif` / multiple `if` statements. -- Know how to convert a floating-point number to an integer by truncation. +- Know how to compare numbers with `===`, `!==`, `<`, `>`, `<=`, and `>=`. +- Know how to use the spaceship operator `<=>`. +- Know that most comparison operators return a boolean, while `<=>` returns `-1`, `0`, or `1`. ## Out of scope -- `switch` / `match` expressions. -- The ternary operator. -- Type declarations. -- `declare(strict_types=1)`. +- Teaching `if` / `elseif` / `else` in depth (`if` is hand-waving only). +- `switch` / `match` / ternary. +- Type declarations and `declare(strict_types=1)`. +- Explicit type casting / type juggling (`(int)`, `intval()`, loose `==`). +- Class constants and `self::`. +- Object comparison. ## Concepts -- `comparison-operators`: know how to compare values; know the difference between equal and identical comparisons. -- `if-control-structures`: know how to conditionally execute code using `if` / `elseif` / `else`. +- `comparison-operators` ## Prerequisites -- `booleans`: know how to use boolean values and operators. -- `integers`: know how to work with whole numbers. -- `floating-point-numbers`: know how to work with floating-point numbers. -- `arithmetic-operators`: know how to multiply and divide numbers. +- `booleans` +- `integers` +- `floating-point-numbers` + +Arithmetic multiplication is assumed from earlier exercises. + +## Tasks and operators practiced + +1. `successRate($speed)`: `===`, `>=`, `>` (or equivalent range comparisons) with simple `if` hand-waving. +2. `productionRatePerHour($speed)`: reuse `successRate`; multiply with `221` (no casting). +3. `isLineRunning($speed)`: `!==`. +4. `compareSpeeds($left, $right)`: `<=>`. ## Analyzer -This exercise could benefit from the following rules: +Possible future rules: -- `actionable`: If the student did not reuse `successRate` inside `productionRatePerHour`, instruct them to do so. -- `informative`: If the solution repeatedly hard-codes `221`, suggest storing it in a constant. -- `informative`: If the solution uses `if`/`elseif` with early returns, note that trailing `else` branches may be redundant. +- `actionable`: suggest reusing `successRate` inside `productionRatePerHour`. +- `informative`: if `compareSpeeds` uses nested `if` instead of `<=>`, suggest the spaceship operator. +- `informative`: if `isLineRunning` wraps a comparison in unnecessary `if` / `return true/false`, suggest returning the comparison directly. diff --git a/exercises/concept/cars-assemble/.meta/exemplar.php b/exercises/concept/cars-assemble/.meta/exemplar.php index 9bd062f55..196292492 100644 --- a/exercises/concept/cars-assemble/.meta/exemplar.php +++ b/exercises/concept/cars-assemble/.meta/exemplar.php @@ -2,8 +2,6 @@ class CarsAssemble { - private const CARS_PER_HOUR = 221; - public function successRate($speed) { if ($speed === 10) { @@ -18,7 +16,7 @@ public function successRate($speed) return 0.9; } - if ($speed >= 1) { + if ($speed > 0) { return 1.0; } @@ -27,11 +25,16 @@ public function successRate($speed) public function productionRatePerHour($speed) { - return self::CARS_PER_HOUR * $speed * $this->successRate($speed); + return 221 * $speed * $this->successRate($speed); + } + + public function isLineRunning($speed) + { + return $speed !== 0; } - public function workingItemsPerMinute($speed) + public function compareSpeeds($left, $right) { - return (int) ($this->productionRatePerHour($speed) / 60); + return $left <=> $right; } } diff --git a/exercises/concept/cars-assemble/CarsAssemble.php b/exercises/concept/cars-assemble/CarsAssemble.php index cb5b5c182..0544f0350 100644 --- a/exercises/concept/cars-assemble/CarsAssemble.php +++ b/exercises/concept/cars-assemble/CarsAssemble.php @@ -12,7 +12,12 @@ public function productionRatePerHour($speed) throw new \BadFunctionCallException("Implement the function"); } - public function workingItemsPerMinute($speed) + public function isLineRunning($speed) + { + throw new \BadFunctionCallException("Implement the function"); + } + + public function compareSpeeds($left, $right) { throw new \BadFunctionCallException("Implement the function"); } diff --git a/exercises/concept/cars-assemble/CarsAssembleTest.php b/exercises/concept/cars-assemble/CarsAssembleTest.php index 5fb73066d..f11b76250 100644 --- a/exercises/concept/cars-assemble/CarsAssembleTest.php +++ b/exercises/concept/cars-assemble/CarsAssembleTest.php @@ -54,6 +54,17 @@ public function testSuccessRateForSpeedFive() $this->assertEqualsWithDelta(0.9, $actual, 0.001); } + /** + * @task_id 1 + */ + #[TestDox('Success rate for speed 8')] + public function testSuccessRateForSpeedEight() + { + $assembly_line = new CarsAssemble(); + $actual = $assembly_line->successRate(8); + $this->assertEqualsWithDelta(0.9, $actual, 0.001); + } + /** * @task_id 1 */ @@ -84,7 +95,7 @@ public function testProductionRatePerHourForSpeedZero() { $assembly_line = new CarsAssemble(); $actual = $assembly_line->productionRatePerHour(0); - $this->assertEqualsWithDelta(0.0, $actual, 0.1); + $this->assertEqualsWithDelta(0.0, $actual, 0.001); } /** @@ -95,7 +106,7 @@ public function testProductionRatePerHourForSpeedOne() { $assembly_line = new CarsAssemble(); $actual = $assembly_line->productionRatePerHour(1); - $this->assertEqualsWithDelta(221.0, $actual, 0.1); + $this->assertEqualsWithDelta(221.0, $actual, 0.001); } /** @@ -106,18 +117,18 @@ public function testProductionRatePerHourForSpeedFour() { $assembly_line = new CarsAssemble(); $actual = $assembly_line->productionRatePerHour(4); - $this->assertEqualsWithDelta(884.0, $actual, 0.1); + $this->assertEqualsWithDelta(884.0, $actual, 0.001); } /** * @task_id 2 */ - #[TestDox('Production rate per hour for speed 7')] - public function testProductionRatePerHourForSpeedSeven() + #[TestDox('Production rate per hour for speed 6')] + public function testProductionRatePerHourForSpeedSix() { $assembly_line = new CarsAssemble(); - $actual = $assembly_line->productionRatePerHour(7); - $this->assertEqualsWithDelta(1392.3, $actual, 0.1); + $actual = $assembly_line->productionRatePerHour(6); + $this->assertEqualsWithDelta(1193.4, $actual, 0.001); } /** @@ -128,7 +139,7 @@ public function testProductionRatePerHourForSpeedNine() { $assembly_line = new CarsAssemble(); $actual = $assembly_line->productionRatePerHour(9); - $this->assertEqualsWithDelta(1591.2, $actual, 0.1); + $this->assertEqualsWithDelta(1591.2, $actual, 0.001); } /** @@ -139,72 +150,66 @@ public function testProductionRatePerHourForSpeedTen() { $assembly_line = new CarsAssemble(); $actual = $assembly_line->productionRatePerHour(10); - $this->assertEqualsWithDelta(1701.7, $actual, 0.1); + $this->assertEqualsWithDelta(1701.7, $actual, 0.001); } /** * @task_id 3 */ - #[TestDox('Working items per minute for speed 0')] - public function testWorkingItemsPerMinuteForSpeedZero() + #[TestDox('Line is not running at speed 0')] + public function testIsLineRunningForSpeedZero() { $assembly_line = new CarsAssemble(); - $actual = $assembly_line->workingItemsPerMinute(0); - $this->assertSame(0, $actual); + $this->assertFalse($assembly_line->isLineRunning(0)); } /** * @task_id 3 */ - #[TestDox('Working items per minute for speed 1')] - public function testWorkingItemsPerMinuteForSpeedOne() + #[TestDox('Line is running at speed 1')] + public function testIsLineRunningForSpeedOne() { $assembly_line = new CarsAssemble(); - $actual = $assembly_line->workingItemsPerMinute(1); - $this->assertSame(3, $actual); + $this->assertTrue($assembly_line->isLineRunning(1)); } /** * @task_id 3 */ - #[TestDox('Working items per minute for speed 5')] - public function testWorkingItemsPerMinuteForSpeedFive() + #[TestDox('Line is running at speed 10')] + public function testIsLineRunningForSpeedTen() { $assembly_line = new CarsAssemble(); - $actual = $assembly_line->workingItemsPerMinute(5); - $this->assertSame(16, $actual); + $this->assertTrue($assembly_line->isLineRunning(10)); } /** - * @task_id 3 + * @task_id 4 */ - #[TestDox('Working items per minute for speed 8')] - public function testWorkingItemsPerMinuteForSpeedEight() + #[TestDox('Compare speeds when the first is smaller')] + public function testCompareSpeedsWhenFirstIsSmaller() { $assembly_line = new CarsAssemble(); - $actual = $assembly_line->workingItemsPerMinute(8); - $this->assertSame(26, $actual); + $this->assertSame(-1, $assembly_line->compareSpeeds(3, 7)); } /** - * @task_id 3 + * @task_id 4 */ - #[TestDox('Working items per minute for speed 9')] - public function testWorkingItemsPerMinuteForSpeedNine() + #[TestDox('Compare speeds when both are equal')] + public function testCompareSpeedsWhenEqual() { $assembly_line = new CarsAssemble(); - $actual = $assembly_line->workingItemsPerMinute(9); - $this->assertSame(26, $actual); + $this->assertSame(0, $assembly_line->compareSpeeds(5, 5)); } /** - * @task_id 3 + * @task_id 4 */ - #[TestDox('Working items per minute for speed 10')] - public function testWorkingItemsPerMinuteForSpeedTen() + #[TestDox('Compare speeds when the first is greater')] + public function testCompareSpeedsWhenFirstIsGreater() { $assembly_line = new CarsAssemble(); - $actual = $assembly_line->workingItemsPerMinute(10); - $this->assertSame(28, $actual); + $this->assertSame(1, $assembly_line->compareSpeeds(9, 2)); } } From 189814a8e29bc540a8bc499c349ffeada6ba07d6 Mon Sep 17 00:00:00 2001 From: Pablo Miralles Date: Thu, 30 Jul 2026 11:46:28 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: mk-mxp <55182845+mk-mxp@users.noreply.github.com> --- concepts/comparison-operators/about.md | 4 ++-- concepts/comparison-operators/introduction.md | 4 ++-- exercises/concept/cars-assemble/.docs/hints.md | 5 +++-- exercises/concept/cars-assemble/.docs/instructions.md | 6 +++--- exercises/concept/cars-assemble/.meta/config.json | 1 + exercises/concept/cars-assemble/CarsAssemble.php | 2 +- exercises/concept/cars-assemble/CarsAssembleTest.php | 8 ++++---- 7 files changed, 16 insertions(+), 14 deletions(-) diff --git a/concepts/comparison-operators/about.md b/concepts/comparison-operators/about.md index 02521f742..3e97c3f0d 100644 --- a/concepts/comparison-operators/about.md +++ b/concepts/comparison-operators/about.md @@ -30,10 +30,10 @@ With `===`, both the value and the type must match. ```php true, equal after type juggling +1 == "1"; // => true, equal after implicit conversion of string to int 1 === "1"; // => false, not identical -1 == 1.0; // => true +1 == 1.0; // => true, equal after implicit conversion of int to float 1 === 1.0; // => false ``` diff --git a/concepts/comparison-operators/introduction.md b/concepts/comparison-operators/introduction.md index b46314694..2f61a1785 100644 --- a/concepts/comparison-operators/introduction.md +++ b/concepts/comparison-operators/introduction.md @@ -3,7 +3,7 @@ Comparison operators compare two values and usually return a boolean (`true` or `false`). They are commonly used to make decisions in code. -For learning PHP, start with **identical** comparisons and relational comparisons between numbers: +PHP has **identical** comparisons and relational comparisons between numbers: ```php $b` | greater than | | `$a <= $b` | less than or equal to | diff --git a/exercises/concept/cars-assemble/.docs/hints.md b/exercises/concept/cars-assemble/.docs/hints.md index bf9343a84..9d3bce497 100644 --- a/exercises/concept/cars-assemble/.docs/hints.md +++ b/exercises/concept/cars-assemble/.docs/hints.md @@ -7,8 +7,9 @@ ## 1. Calculate the success rate -- Compare `$speed` with the boundary values from the table (`===`, `>=`, `>`, `<=`, or `<` as needed). -- You can use a simple `if` statement to return the matching success rate. +- Compare `$speed` with the boundary values from the table. +- Use `identity`, `greater than`, `greater or equal`, `lower than`, `lower or equal` operators as needed. +- You can use an `if` statement to return the matching success rate. ## 2. Calculate the production rate per hour diff --git a/exercises/concept/cars-assemble/.docs/instructions.md b/exercises/concept/cars-assemble/.docs/instructions.md index 85032ed6d..be00557af 100644 --- a/exercises/concept/cars-assemble/.docs/instructions.md +++ b/exercises/concept/cars-assemble/.docs/instructions.md @@ -24,8 +24,8 @@ Use comparisons to choose the correct rate from this table: ```php successRate(10); +$assemblyLine = new CarsAssemble(); +$assemblyLine->successRate(10); // => 0.77 ``` @@ -45,7 +45,7 @@ $assembly_line->productionRatePerHour(6); ## 3. Check whether the line is running Implement the `isLineRunning()` method to return whether the assembly line is running. -The line is running when its speed is **not identical** to `0`. +The line is running when its speed is **not identical** to the `off` speed (`0`). ```php successRate(0); $this->assertEqualsWithDelta(0.0, $actual, 0.001); } @@ -90,7 +90,7 @@ public function testSuccessRateForSpeedTen() /** * @task_id 2 */ - #[TestDox('Production rate per hour for speed 0')] + #[TestDox('Production rate per hour for "off" (speed 0)')] public function testProductionRatePerHourForSpeedZero() { $assembly_line = new CarsAssemble(); @@ -156,7 +156,7 @@ public function testProductionRatePerHourForSpeedTen() /** * @task_id 3 */ - #[TestDox('Line is not running at speed 0')] + #[TestDox('Line is not running when "off" (speed 0)')] public function testIsLineRunningForSpeedZero() { $assembly_line = new CarsAssemble(); From bbcfa0e2a698df916486a6c6a1819e4d58a6761a Mon Sep 17 00:00:00 2001 From: Pablo Miralles Date: Thu, 30 Jul 2026 12:01:27 +0200 Subject: [PATCH 4/4] Use camelCase for assemblyLine in docs and tests. Co-authored-by: Cursor --- .../cars-assemble/.docs/instructions.md | 18 ++--- .../cars-assemble/CarsAssembleTest.php | 74 +++++++++---------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/exercises/concept/cars-assemble/.docs/instructions.md b/exercises/concept/cars-assemble/.docs/instructions.md index be00557af..bd2d4b21f 100644 --- a/exercises/concept/cars-assemble/.docs/instructions.md +++ b/exercises/concept/cars-assemble/.docs/instructions.md @@ -37,8 +37,8 @@ Multiply the base production (`221` cars per hour at speed `1`) by the speed and ```php productionRatePerHour(6); +$assemblyLine = new CarsAssemble(); +$assemblyLine->productionRatePerHour(6); // => 1193.4 ``` @@ -50,11 +50,11 @@ The line is running when its speed is **not identical** to the `off` speed (`0`) ```php isLineRunning(0); +$assemblyLine = new CarsAssemble(); +$assemblyLine->isLineRunning(0); // => false -$assembly_line->isLineRunning(3); +$assemblyLine->isLineRunning(3); // => true ``` @@ -70,13 +70,13 @@ It should return: ```php compareSpeeds(3, 7); +$assemblyLine = new CarsAssemble(); +$assemblyLine->compareSpeeds(3, 7); // => -1 -$assembly_line->compareSpeeds(5, 5); +$assemblyLine->compareSpeeds(5, 5); // => 0 -$assembly_line->compareSpeeds(9, 2); +$assemblyLine->compareSpeeds(9, 2); // => 1 ``` diff --git a/exercises/concept/cars-assemble/CarsAssembleTest.php b/exercises/concept/cars-assemble/CarsAssembleTest.php index 58227e80e..1436c56c5 100644 --- a/exercises/concept/cars-assemble/CarsAssembleTest.php +++ b/exercises/concept/cars-assemble/CarsAssembleTest.php @@ -17,7 +17,7 @@ public static function setUpBeforeClass(): void public function testSuccessRateForSpeedZero() { $assemblyLine = new CarsAssemble(); - $actual = $assembly_line->successRate(0); + $actual = $assemblyLine->successRate(0); $this->assertEqualsWithDelta(0.0, $actual, 0.001); } @@ -27,8 +27,8 @@ public function testSuccessRateForSpeedZero() #[TestDox('Success rate for speed 1')] public function testSuccessRateForSpeedOne() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->successRate(1); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->successRate(1); $this->assertEqualsWithDelta(1.0, $actual, 0.001); } @@ -38,8 +38,8 @@ public function testSuccessRateForSpeedOne() #[TestDox('Success rate for speed 4')] public function testSuccessRateForSpeedFour() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->successRate(4); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->successRate(4); $this->assertEqualsWithDelta(1.0, $actual, 0.001); } @@ -49,8 +49,8 @@ public function testSuccessRateForSpeedFour() #[TestDox('Success rate for speed 5')] public function testSuccessRateForSpeedFive() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->successRate(5); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->successRate(5); $this->assertEqualsWithDelta(0.9, $actual, 0.001); } @@ -60,8 +60,8 @@ public function testSuccessRateForSpeedFive() #[TestDox('Success rate for speed 8')] public function testSuccessRateForSpeedEight() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->successRate(8); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->successRate(8); $this->assertEqualsWithDelta(0.9, $actual, 0.001); } @@ -71,8 +71,8 @@ public function testSuccessRateForSpeedEight() #[TestDox('Success rate for speed 9')] public function testSuccessRateForSpeedNine() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->successRate(9); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->successRate(9); $this->assertEqualsWithDelta(0.8, $actual, 0.001); } @@ -82,8 +82,8 @@ public function testSuccessRateForSpeedNine() #[TestDox('Success rate for speed 10')] public function testSuccessRateForSpeedTen() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->successRate(10); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->successRate(10); $this->assertEqualsWithDelta(0.77, $actual, 0.001); } @@ -93,8 +93,8 @@ public function testSuccessRateForSpeedTen() #[TestDox('Production rate per hour for "off" (speed 0)')] public function testProductionRatePerHourForSpeedZero() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->productionRatePerHour(0); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->productionRatePerHour(0); $this->assertEqualsWithDelta(0.0, $actual, 0.001); } @@ -104,8 +104,8 @@ public function testProductionRatePerHourForSpeedZero() #[TestDox('Production rate per hour for speed 1')] public function testProductionRatePerHourForSpeedOne() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->productionRatePerHour(1); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->productionRatePerHour(1); $this->assertEqualsWithDelta(221.0, $actual, 0.001); } @@ -115,8 +115,8 @@ public function testProductionRatePerHourForSpeedOne() #[TestDox('Production rate per hour for speed 4')] public function testProductionRatePerHourForSpeedFour() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->productionRatePerHour(4); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->productionRatePerHour(4); $this->assertEqualsWithDelta(884.0, $actual, 0.001); } @@ -126,8 +126,8 @@ public function testProductionRatePerHourForSpeedFour() #[TestDox('Production rate per hour for speed 6')] public function testProductionRatePerHourForSpeedSix() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->productionRatePerHour(6); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->productionRatePerHour(6); $this->assertEqualsWithDelta(1193.4, $actual, 0.001); } @@ -137,8 +137,8 @@ public function testProductionRatePerHourForSpeedSix() #[TestDox('Production rate per hour for speed 9')] public function testProductionRatePerHourForSpeedNine() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->productionRatePerHour(9); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->productionRatePerHour(9); $this->assertEqualsWithDelta(1591.2, $actual, 0.001); } @@ -148,8 +148,8 @@ public function testProductionRatePerHourForSpeedNine() #[TestDox('Production rate per hour for speed 10')] public function testProductionRatePerHourForSpeedTen() { - $assembly_line = new CarsAssemble(); - $actual = $assembly_line->productionRatePerHour(10); + $assemblyLine = new CarsAssemble(); + $actual = $assemblyLine->productionRatePerHour(10); $this->assertEqualsWithDelta(1701.7, $actual, 0.001); } @@ -159,8 +159,8 @@ public function testProductionRatePerHourForSpeedTen() #[TestDox('Line is not running when "off" (speed 0)')] public function testIsLineRunningForSpeedZero() { - $assembly_line = new CarsAssemble(); - $this->assertFalse($assembly_line->isLineRunning(0)); + $assemblyLine = new CarsAssemble(); + $this->assertFalse($assemblyLine->isLineRunning(0)); } /** @@ -169,8 +169,8 @@ public function testIsLineRunningForSpeedZero() #[TestDox('Line is running at speed 1')] public function testIsLineRunningForSpeedOne() { - $assembly_line = new CarsAssemble(); - $this->assertTrue($assembly_line->isLineRunning(1)); + $assemblyLine = new CarsAssemble(); + $this->assertTrue($assemblyLine->isLineRunning(1)); } /** @@ -179,8 +179,8 @@ public function testIsLineRunningForSpeedOne() #[TestDox('Line is running at speed 10')] public function testIsLineRunningForSpeedTen() { - $assembly_line = new CarsAssemble(); - $this->assertTrue($assembly_line->isLineRunning(10)); + $assemblyLine = new CarsAssemble(); + $this->assertTrue($assemblyLine->isLineRunning(10)); } /** @@ -189,8 +189,8 @@ public function testIsLineRunningForSpeedTen() #[TestDox('Compare speeds when the first is smaller')] public function testCompareSpeedsWhenFirstIsSmaller() { - $assembly_line = new CarsAssemble(); - $this->assertSame(-1, $assembly_line->compareSpeeds(3, 7)); + $assemblyLine = new CarsAssemble(); + $this->assertSame(-1, $assemblyLine->compareSpeeds(3, 7)); } /** @@ -199,8 +199,8 @@ public function testCompareSpeedsWhenFirstIsSmaller() #[TestDox('Compare speeds when both are equal')] public function testCompareSpeedsWhenEqual() { - $assembly_line = new CarsAssemble(); - $this->assertSame(0, $assembly_line->compareSpeeds(5, 5)); + $assemblyLine = new CarsAssemble(); + $this->assertSame(0, $assemblyLine->compareSpeeds(5, 5)); } /** @@ -209,7 +209,7 @@ public function testCompareSpeedsWhenEqual() #[TestDox('Compare speeds when the first is greater')] public function testCompareSpeedsWhenFirstIsGreater() { - $assembly_line = new CarsAssemble(); - $this->assertSame(1, $assembly_line->compareSpeeds(9, 2)); + $assemblyLine = new CarsAssemble(); + $this->assertSame(1, $assemblyLine->compareSpeeds(9, 2)); } }