Skip to content

Commit

Permalink
Merge pull request #57 from MaxGoryunov/feature/52
Browse files Browse the repository at this point in the history
Feature/52
  • Loading branch information
MaxGoryunov committed Aug 3, 2021
2 parents 2b75741 + 55184a7 commit 47db60b
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 62 deletions.
11 changes: 10 additions & 1 deletion codecov.yml
@@ -1,7 +1,16 @@
codecov:
require_ci_to_pass: yes

# @todo #52:15min add codecov strict analysis for 100% coverage.
coverage:
status:
patch:
default:
target: auto
threshold: 10%
project:
default:
target: auto
threshold: 10%
precision: 2
round: down
range: "70...100"
Expand Down
43 changes: 12 additions & 31 deletions fakes/Let.php
Expand Up @@ -3,50 +3,31 @@
namespace MaxGoryunov\SavingIterator\Fakes;

use Closure;
use MaxGoryunov\SavingIterator\Src\Scalar;

/**
* Allows to use context instead of creating a new variable.
*
* @todo #44:25min Classes Let and The contain some repeated cdde which could
* be extracted into a separate class.
* @template X subject type
* @template Y result type
* @implements \MaxGoryunov\SavingIterator\Src\Scalar<Y>
* @extends SurveyEnvelope<X, Y>
*/
class Let implements Scalar
class Let extends SurveyEnvelope
{

/**
* Ctor.
*
* @param X $subject
* @param Closure(X): Y $context
*/
public function __construct(
/**
* Element to be put into the context.
*
* @var X
*/
private mixed $subject,

/**
* Context for the element.
*
* @var Closure(X): Y
*/
private Closure $context
) {
}

/**
* Returns result of applying context to element.
*
* @return Y
* @phpstan-param X $subject repeated element
* @phpstan-param Closure(X): Y $context context for element
* @param mixed $subject element to be put into context
* @param Closure $context context for element
*/
public function value(): mixed
public function __construct(mixed $subject, Closure $context)
{
return ($this->context)($this->subject);
parent::__construct(
$subject,
$context,
fn(mixed $subject, Closure $context): mixed => $context($subject)
);
}
}
64 changes: 64 additions & 0 deletions fakes/SurveyEnvelope.php
@@ -0,0 +1,64 @@
<?php

namespace MaxGoryunov\SavingIterator\Fakes;

use Closure;
use MaxGoryunov\SavingIterator\Src\Scalar;

/**
* Class for wrapping divergent program flows. It allows to structure a program
* in such a way that there will be no need for a variable if it exists only in
* order to be used in two places.
* @todo #52:20min Create test file for this class and maybe change tests for
* `Let` and `The`.
* @template X subject type
* @template Y result type
* @implements \MaxGoryunov\SavingIterator\Src\Scalar<Y>
*/
abstract class SurveyEnvelope implements Scalar
{

/**
* Ctor.
*
* @phpstan-param X $subject
* @phpstan-param Closure(X): mixed $context
* @phpstan-param Closure(X, Closure(X): mixed): Y $usage
* @param mixed $subject element to be used in some context
* @param Closure $context context for element
* @param Closure $usage way of combining element and context
*/
public function __construct(
/**
* Element to be used in some context.
*
* @var X
*/
private mixed $subject,

/**
* Context for element.
*
* @var Closure(X): mixed
*/
private Closure $context,

/**
* way of combining element and context.
*
* @var Closure(X, Closure(X): mixed): Y
*/
private Closure $usage
) {
}

/**
* Returns result of applying context to element.
*
* @return Y
*/
public final function value(): mixed
{
return ($this->usage)($this->subject, $this->context);
}
}
46 changes: 16 additions & 30 deletions fakes/The.php
Expand Up @@ -3,48 +3,34 @@
namespace MaxGoryunov\SavingIterator\Fakes;

use Closure;
use MaxGoryunov\SavingIterator\Src\Scalar;

/**
* Class for applying contexts to elements without changing them.
*
* @template T subject type
* @implements \MaxGoryunov\SavingIterator\Src\Scalar<T>
* @extends SurveyEnvelope<T, T>
*/
class The implements Scalar
class The extends SurveyEnvelope
{

/**
* Ctor.
*
* @param T $subject
* @param Closure(T): mixed $context
*/
public function __construct(
/**
* Element to be put into the context.
*
* @var T
*/
private mixed $subject,

/**
* Context for the element.
*
* @var Closure(T): mixed
*/
private Closure $context
) {
}

/**
* Applies context to subject and returns subject.
*
* @return T
* @phpstan-param T $subject repeating element
* @phpstan-param Closure(T): mixed $context context for element
* @param mixed $subject
* @param Closure $context
*/
public function value(): mixed
public function __construct(mixed $subject, Closure $context)
{
($this->context)($this->subject);
return $this->subject;
parent::__construct(
$subject,
$context,
function (mixed $subject, Closure $context): mixed
{
$context($subject);
return $subject;
}
);
}
}
2 changes: 2 additions & 0 deletions tests/fakes/LetTest.php
Expand Up @@ -15,6 +15,8 @@ class LetTest extends TestCase
* @covers ::__construct
* @covers ::value
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
*
* @small
*
* @return void
Expand Down
1 change: 1 addition & 0 deletions tests/fakes/TheTest.php
Expand Up @@ -16,6 +16,7 @@ class TheTest extends TestCase
* @covers ::__construct
* @covers ::value
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\Let
*
* @small
Expand Down
7 changes: 7 additions & 0 deletions tests/src/SavingIteratorTest.php
Expand Up @@ -35,6 +35,7 @@ class SavingIteratorTest extends TestCase
* @covers ::key
* @covers ::next
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
*
* @small
Expand Down Expand Up @@ -63,6 +64,7 @@ public function testIteratesWithGivenIterator(): void
* @covers ::key
* @covers ::next
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
*
* @small
Expand Down Expand Up @@ -107,6 +109,7 @@ public function testDoesNotCallOriginIfValuesAreInCache(): void
* @covers ::key
* @covers ::next
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
*
* @small
Expand Down Expand Up @@ -141,6 +144,7 @@ public function testWorksWithGenerator(): void
* @covers ::key
* @covers ::next
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
*
* @small
Expand Down Expand Up @@ -201,6 +205,7 @@ public function testWorksWithEmptyGenerator(): void
* @covers ::key
* @covers ::next
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
*
* @small
Expand Down Expand Up @@ -228,6 +233,7 @@ public function testIterationsGiveSameResults(): void
* @covers ::key
* @covers ::next
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
*
* @small
Expand Down Expand Up @@ -290,6 +296,7 @@ public function testWorksWithEmptyIterator(): void
* @covers ::key
* @covers ::next
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
* @uses MaxGoryunov\SavingIterator\Src\TimesCalled
* @uses MaxGoryunov\SavingIterator\Src\TransparentIterator
Expand Down
2 changes: 2 additions & 0 deletions tests/src/TimesCalledTest.php
Expand Up @@ -19,6 +19,7 @@ class TimesCalledTest extends TestCase
* @covers ::__call
* @covers ::value
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
* @uses MaxGoryunov\SavingIterator\Fakes\Let
*
Expand Down Expand Up @@ -55,6 +56,7 @@ function (TimesCalled $called) use ($method, $times) {
* @covers ::__call
* @covers ::value
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
* @uses MaxGoryunov\SavingIterator\Fakes\Let
*
Expand Down
1 change: 1 addition & 0 deletions tests/src/TransparentIteratorTest.php
Expand Up @@ -21,6 +21,7 @@ class TransparentIteratorTest extends TestCase
* @covers ::rewind
* @covers ::next
*
* @uses MaxGoryunov\SavingIterator\Fakes\SurveyEnvelope
* @uses MaxGoryunov\SavingIterator\Fakes\The
*
* @small
Expand Down

3 comments on commit 47db60b

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 47db60b Aug 3, 2021

Choose a reason for hiding this comment

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

Puzzle 44-cdf39217 disappeared from fakes/Let.php, that's why I closed #52. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 47db60b Aug 3, 2021

Choose a reason for hiding this comment

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

Puzzle 52-3176158a discovered in fakes/SurveyEnvelope.php and submitted as #68. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 47db60b Aug 3, 2021

Choose a reason for hiding this comment

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

Puzzle 52-c17a2651 discovered in codecov.yml and submitted as #69. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.