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

Add doc blocks, type hinting. Make stricter. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 31 additions & 9 deletions src/Curried.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@ class Curried
*/
private $func;

/**
* @var array
Copy link
Owner

Choose a reason for hiding this comment

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

yeah I'm going to veto this bit of the change. It doesn't seem to add any additional information

Copy link
Author

Choose a reason for hiding this comment

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

How about mixed[] - it would indicate an Array of variable types 🤔

Copy link
Owner

Choose a reason for hiding this comment

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

good point. I think I like mixed[] better as it's more explicit that we expect a mix rather than just didn't bother specifying the type.

*/
private $args = [];

/**
span class="pl-s1"> * @var integer
Copy link
Owner

Choose a reason for hiding this comment

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

I think you may have slipped on your keyboard here 😆 🤣

*/
private $arity;

public function __construct($callable, $arity = null)
/**
* Curried constructor.
Copy link
Owner

Choose a reason for hiding this comment

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

The @param s I like but I'd remove 'Curried constructor.' as it doesn't add any extra information,

Copy link
Author

Choose a reason for hiding this comment

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

PHPStorm default - don't even notice them any more. Can remove 😄

Copy link
Owner

Choose a reason for hiding this comment

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

yeah it's a pet peeve of mine comments that say things like "add one to x". I probably spend more time than is sensible editing phpstorm's defaults.

*
* @param callable $callable
* @param int|null $arity
* @throws \ReflectionException
*/
public function __construct(callable $callable, $arity = null)
{
if (!is_callable($callable)) {
throw new \InvalidArgumentException("Curried can only wrap a callable");
Expand All @@ -24,18 +38,26 @@ public function __construct($callable, $arity = null)
public function __invoke()
{
$arguments = func_get_args();
if (count($this->args) == $this->arity - count($arguments)) {
if (count($this->args) === $this->arity - count($arguments)) {
Copy link
Owner

Choose a reason for hiding this comment

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

mmmm strict 👍

return call_user_func_array($this->func, array_merge($this->args, $arguments));
} else {
$curried = new Curried($this->func, $this->arity);
foreach($arguments as $argument) {
$curried->args[] = $argument;
}
return $curried;
}

$curried = new Curried($this->func, $this->arity);

foreach($arguments as $argument) {
$curried->args[] = $argument;
}

return $curried;
}

private function setArity($callable, $arity = null)
/**
* @param callable $callable
* @param int|null $arity
* @return void
* @throws \ReflectionException
*/
private function setArity(callable $callable, $arity = null)
{
if ($arity !== null) {
$this->arity = $arity;
Expand Down
15 changes: 15 additions & 0 deletions src/CurryWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,31 @@

class CurryWrapper
{
/**
* @var mixed
*/
private $object;

/**
* CurryWrapper constructor.
Copy link
Owner

Choose a reason for hiding this comment

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

I'd remove the string "CurryWrapper" constructor as it doesn't add any extra information.

*
* @param mixed $object
*/
public function __construct($object)
{
$this->object = $object;
}

/**
* @param string $functionName
* @param array $arguments
* @return mixed
* @throws \ReflectionException
*/
public function __call($functionName, array $arguments)
{
$curried = new Curried([$this->object, $functionName]);

return call_user_func_array($curried, $arguments);
}
}
6 changes: 3 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function curry($thing)
{
if (is_object($thing) && !($thing instanceof \Closure)) {
return new CurryWrapper($thing);
} else {
return new Curried($thing);
}
}

return new Curried($thing);
}