Skip to content

Commit 92cf8dd

Browse files
committed
Extract types
1 parent fb209f1 commit 92cf8dd

25 files changed

+127
-415
lines changed

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
/vendor/
2-
composer.lock
1+
.idea
2+
vendor
3+
composer.lock
4+
build

.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
sudo: false
3+
php:
4+
- 7.1
5+
- 7.2
6+
7+
before_install:
8+
- composer self-update
9+
10+
install:
11+
- travis_retry composer install --no-interaction --prefer-source
12+
13+
script:
14+
- vendor/bin/phpstan analyze src/ tests/ --level max
15+
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
16+
17+
after_success:
18+
- travis_retry php vendor/bin/php-coveralls -v

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2018 Vsevolod Vietluzhskykh
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Overloaded Function
22

3+
[![Build Status](https://travis-ci.com/Sevavietl/OverloadedFunction.svg?branch=master)](https://travis-ci.com/Sevavietl/OverloadedFunction)
4+
[![Coverage Status](https://coveralls.io/repos/github/Sevavietl/OverloadedFunction/badge.svg)](https://coveralls.io/github/Sevavietl/OverloadedFunction)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6+
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/phpstan/phpstan)
7+
38
PHP actually does not support [function overloading](https://en.wikipedia.org/wiki/Function_overloading), as functions distinguished but name only.
49
This is a class that helps to imulate function overloading in PHP.
510

composer.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
"email": "sevavietl@gmail.com"
1616
}
1717
],
18-
"require": {},
18+
"require": {
19+
"php": ">=7.1",
20+
"sevavietl/type-guard": "^0.1.1"
21+
},
1922
"require-dev": {
20-
"phpunit/phpunit": "^6.3"
23+
"phpunit/phpunit": "^6.3",
24+
"phpstan/phpstan": "^0.10.3",
25+
"php-coveralls/php-coveralls": "^2.1"
2126
},
2227
"autoload": {
2328
"psr-4": {

phpunit.xml renamed to phpunit.xml.dist

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
processIsolation="false"
1010
stopOnFailure="false"
1111
syntaxCheck="false">
12+
<filter>
13+
<whitelist processUncoveredFilesFromWhitelist="true">
14+
<directory suffix=".php">./src</directory>
15+
</whitelist>
16+
</filter>
1217
<testsuites>
1318
<testsuite name="unit">
1419
<directory suffix="Test.php">tests/unit/</directory>

src/FunctionHasNoCasesException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Sevavietl\OverloadedFunction;
44

5-
class FunctionHasNoCasesException extends \DomainException
5+
final class FunctionHasNoCasesException extends \DomainException
66
{
77

88
}

src/OverloadedFunction.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Sevavietl\OverloadedFunction;
44

5-
use Sevavietl\OverloadedFunction\Signature\Signature;
5+
use TypeGuard\Guard;
66

7-
class OverloadedFunction
7+
final class OverloadedFunction
88
{
9+
/** @var callable */
910
private $func;
1011

1112
public function __construct(array $cases)
@@ -17,7 +18,7 @@ public function __construct(array $cases)
1718
$this->prepareFunction($this->prepareCases($cases));
1819
}
1920

20-
private function prepareCases($cases)
21+
private function prepareCases(array $cases): \SplObjectStorage
2122
{
2223
$_cases = new \SplObjectStorage;
2324

@@ -28,19 +29,24 @@ private function prepareCases($cases)
2829
return $_cases;
2930
}
3031

31-
private function prepareFunction(\SplObjectStorage $cases)
32+
private function prepareFunction(\SplObjectStorage $cases): void
3233
{
3334
$this->func = function (...$args) use ($cases) {
35+
/** @var Guard $signature */
3436
foreach ($cases as $signature) {
3537
if ($signature->match($args)) {
3638
return $cases[$signature](...$args);
3739
}
3840
}
3941

40-
throw new UnknownSignatureException("There is no function case for provided parameters.");
42+
throw new UnknownSignatureException('There is no function case for provided parameters.');
4143
};
4244
}
4345

46+
/**
47+
* @param mixed ...$argv
48+
* @return mixed
49+
*/
4450
public function __invoke(...$argv)
4551
{
4652
return ($this->func)(...$argv);

src/Signature.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Sevavietl\OverloadedFunction;
4+
5+
use TypeGuard\Guard;
6+
7+
final class Signature
8+
{
9+
/** @var Guard[] */
10+
private $guards;
11+
12+
public function __construct(string $stringRepresentation)
13+
{
14+
$this->parseStringRepresentation($stringRepresentation);
15+
}
16+
17+
private function parseStringRepresentation(string $stringRepresentation): void
18+
{
19+
$paramTypes = array_filter(array_map('trim', explode(',', $stringRepresentation)));
20+
21+
$this->guards = array_map(function (string $paramType): Guard {
22+
return new Guard($paramType);
23+
}, $paramTypes);
24+
}
25+
26+
public function match(array $params)
27+
{
28+
return array_reduce(
29+
array_map(function (?Guard $guard, $param): bool {
30+
if (null === $guard) {
31+
return false;
32+
}
33+
34+
return $guard->match($param);
35+
}, $this->guards, $params),
36+
function ($carry, $result) {
37+
return $carry && $result;
38+
},
39+
true
40+
);
41+
}
42+
}

src/Signature/Signature.php

-86
This file was deleted.

src/Signature/Types/ArrayType.php

-30
This file was deleted.

src/Signature/Types/IType.php

-8
This file was deleted.

src/Signature/Types/IntersectionType.php

-22
This file was deleted.

src/Signature/Types/OptionalType.php

-24
This file was deleted.

src/Signature/Types/Type.php

-22
This file was deleted.

0 commit comments

Comments
 (0)