Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Accumulate to use namespaces #287

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion exercises/accumulate/example.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DId you leave the example file in the execise's root on purpose? Personally I would place it in src, because there I would look for it and it makes adjusting Makefile a little easier.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought:

Not sure, if the exercise root folder would still be the better place. I have to sleep over it...

Is there an exercise, where the example solution requires multiple files (more than one class)? That would require some thought on how we can solve this. In that case an example directory might be the better solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that more part of how exercism works regardless of the language?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I am mainly on the PHP track for now. We leave it here for now, that's fine.


namespace Exercism\Accumulate;

/**
* Given the input array and operation returns an array
* containing the result of applying that operation
* to each element of the input array.
*
* @param array $input
* @param array $input
* @param callable $accumulator
*
* @return array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Exercism\Accumulate;

function accumulate(array $input, callable $accumulator)
{
// YOUR CODE GOES HERE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
<?php

class AccumulateTest extends PHPUnit\Framework\TestCase
namespace ExercismTest\Accumulate;

use PHPUnit\Framework\TestCase;
use function Exercism\Accumulate\accumulate;

class AccumulateTest extends TestCase
{
public static function setUpBeforeClass() : void
{
require_once 'accumulate.php';
$files = [
'../src/accumulate.php',
'Str.php',
'StrSplitter.php',
'Is.php',
];

foreach ($files as $file) {
require_once __DIR__ . '/' . $file;
}
}

public function testAccumulateEmpty() : void
Expand Down Expand Up @@ -54,15 +68,15 @@ public function testAccumulateConstants() : void

public function testAccumulateWithinAccumulate() : void
{
$chars = ['a', 'b', 'c'];
$digits = [1, 2, 3];
$chars = ['a', 'b', 'c'];
$digits = [1, 2, 3];
$expected = [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3']];

$this->assertEquals(
$expected,
accumulate($chars, function ($char) use ($digits) {
return accumulate($digits, function ($digit) use ($char) {
return $char.$digit;
return $char . $digit;
});
})
);
Expand All @@ -77,40 +91,16 @@ public function testAccumulateUsingBuiltInFunction() : void

public function testAccumulateUsingStaticMethod() : void
{
$this->assertEquals([5, 6], accumulate(['Hello', 'World!'], 'Str::len'));
$this->assertEquals([5, 6], accumulate(['Hello', 'World!'], 'ExercismTest\Accumulate\Str::len'));
}

public function testAccumulateUsingInvoke() : void
{
$this->assertEquals([['f', 'o', 'o']], accumulate(['foo'], new StrSpliter()));
$this->assertEquals([['f', 'o', 'o']], accumulate(['foo'], new StrSplitter()));
}

public function testAccumulateUsingObjectAndArrayNotation() : void
{
$this->assertEquals([true, false, false], accumulate(['Yes', 0, []], [new Is(), 'truthy']));
}
}

class Str
{
public static function len($string) : int
{
return strlen($string);
}
}

class StrSpliter
{
public function __invoke($value)
{
return str_split($value);
}
}

class Is
{
public function truthy($value) : bool
{
return $value ? true : false;
}
}
11 changes: 11 additions & 0 deletions exercises/accumulate/test/Is.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ExercismTest\Accumulate;

class Is
{
public function truthy($value): bool
{
return $value ? true : false;
}
}
11 changes: 11 additions & 0 deletions exercises/accumulate/test/Str.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ExercismTest\Accumulate;

class Str
{
public static function len($string): int
{
return strlen($string);
}
}
11 changes: 11 additions & 0 deletions exercises/accumulate/test/StrSplitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ExercismTest\Accumulate;

class StrSplitter
{
public function __invoke($value)
{
return str_split($value);
}
}