🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 ## to Triangle in the PHP Track

Published at Jul 23 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

## Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

## Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

## Running the tests

1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`. To find the Exercism workspace run

`````` % exercism debug | grep Workspace
``````
2. Get PHPUnit if you don't have it already.

`````` % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
% chmod +x phpunit.phar
``````
3. Execute the tests:

`````` % ./phpunit.phar triangle/triangle_test.php
``````

## Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### triangle_test.php

``````<?php

require "triangle.php";

class TriangleTest extends PHPUnit\Framework\TestCase
{
public function testEquilateralTrianglesHaveEqualSides()
{
\$this->assertEquals(
'equilateral',
(new Triangle(2, 2, 2))->kind()
);
}

public function testLargerEquilateralTrianglesHaveEqualSides()
{
\$this->assertEquals(
'equilateral',
(new Triangle(10, 10, 10))->kind()
);
}

public function testIsoscelesTriangleWhenLastTwoSidesAreEqual()
{
\$this->assertEquals(
'isosceles',
(new Triangle(3, 4, 4))->kind()
);
}

public function testIsoscelesTriangleWhenFirstAndLastSidesAreEqual()
{
\$this->assertEquals(
'isosceles',
(new Triangle(4, 3, 4))->kind()
);
}

public function testIsoscelesTriangleWhenFirstTwoSidesAreEqual()
{
\$this->assertEquals(
'isosceles',
(new Triangle(4, 4, 3))->kind()
);
}

public function testIsoscelesTrianglesWithUnequalSideLargerThanEqualSides()
{
\$this->assertEquals(
'isosceles',
(new Triangle(4, 4, 7))->kind()
);
}

public function testScaleneTrianglesHaveNoEqualSides()
{
\$this->assertEquals(
'scalene',
(new Triangle(3, 4, 5))->kind()
);
}

public function test2aEqualsBPlusCLooksLikeEquilateralButIsNot()
{
\$this->assertEquals(
'scalene',
(new Triangle(5, 4, 6))->kind()
);
}

public function testScaleneTrianglesHaveNoEqualSidesAtLargerScale()
{
\$this->assertEquals(
'scalene',
(new Triangle(10, 11, 12))->kind()
);
}

public function testScaleneTrianglesHaveNoEqualSidesInDescendingOrder()
{
\$this->assertEquals(
'scalene',
(new Triangle(5, 4, 2))->kind()
);
}

public function testVerySmallTrianglesAreLegal()
{
\$this->assertEquals(
'scalene',
(new Triangle(0.4, 0.6, 0.3))->kind()
);
}

public function testTrianglesWithNoSizeAreIllegal()
{
\$this->expectException(Exception::class);

(new Triangle(0, 0, 0))->kind();
}

public function testTrianglesViolatingTriangleInequalityAreIllegal()
{
\$this->expectException(Exception::class);

(new Triangle(1, 1, 3))->kind();
}

public function testTrianglesViolatingTriangleInequalityAreIllegal2()
{
\$this->expectException(Exception::class);

(new Triangle(7, 3, 2))->kind();
}

public function testTrianglesViolatingTriangleInequalityAreIllegal3()
{
\$this->expectException(Exception::class);

(new Triangle(1, 3, 1))->kind();
}
}``````
``````<?php

class Triangle
{
private \$kind;
public function __construct(\$l1,\$l2,\$l3){
if (!\$this->isTriangleValid(\$l1,\$l2,\$l3)) {
throw new Exception("invalid triangle.");
}
if (\$l1 == \$l2 && \$l2 == \$l3) {
\$this->kind = 'equilateral';
return \$this;
}
\$laterals = [\$l1, \$l2, \$l3,
];
sort(\$laterals);
if (\$laterals == \$laterals || \$laterals == \$laterals) {
\$this->kind = 'isosceles';
return \$this;
}
\$this->kind = "scalene";
}

/**
* @return {bool} if triangle is valid or not
*/
public function isTriangleValid(\$l1,\$l2,\$l3){
if (!\$l1 || !\$l2 || !\$l3) {
return false;
}
if (\$l1 + \$l2 <= \$l3 || \$l2 + \$l3 <= \$l1 ||\$l1 + \$l3 <= \$l2) {
return false;
}
return true;
}

public function kind(){
return \$this->kind;
}
}

echo (new Triangle(4, 3, 4))->kind();``````