Passed
Push — master ( 4ca9e9...d2e70b )
by PHPinnacle
02:43
created

future()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of PHPinnacle/Ensign.
4
 *
5
 * (c) PHPinnacle Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types = 1);
12
13
use Amp\Coroutine;
14
use Amp\Failure;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Failure. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Amp\LazyPromise;
16
use Amp\Loop;
17
use Amp\Promise;
18
use Amp\Success;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Success. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
20
/**
21
 * @param callable $callable
22
 *
23
 * @return void
24
 */
25
function loop(callable $callable): void
26
{
27 11
    Loop::run($callable);
28 6
}
29
30
/**
31
 * @param mixed $value
32
 *
33
 * @return bool
34
 */
35
function is_promise($value): bool
36
{
37 9
    if (\is_array($value) && \count($value) === \count(\array_filter($value, '\is_promise'))) {
38 1
        return true;
39
    }
40
41 9
    return $value instanceof Promise;
42
}
43
44
/**
45
 * @param mixed $value
46
 *
47
 * @return Success
48
 */
49
function success($value = null): Success
50
{
51 8
    return new Success($value);
52
}
53
54
/**
55
 * @param Throwable $error
56
 *
57
 * @return Failure
58
 */
59
function failure(\Throwable $error): Failure
60
{
61 1
    return new Failure($error);
62
}
63
64
/**
65
 * @param Generator $generator
66
 *
67
 * @return Coroutine
68
 */
69
function coroutine(\Generator $generator): Coroutine
70
{
71 8
    return new Coroutine($generator);
72
}
73
74
/**
75
 * @param mixed ...$arguments
76
 *
77
 * @return Promise
78
 */
79
function promise(...$arguments): Promise
80
{
81 9
    if (empty($arguments)) {
82
        return \success();
83
    }
84
85 9
    if (\is_promise($arguments)) {
86 1
        return Promise\all($arguments);
0 ignored issues
show
Bug introduced by
The function all was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        return /** @scrutinizer ignore-call */ Promise\all($arguments);
Loading history...
87
    }
88
89 9
    if (\is_callable($arguments[0])) {
90 2
        $first = \array_shift($arguments);
91
92 2
        return Amp\call($first, ...$arguments);
93
    }
94
95 7
    return isset($arguments[1]) ? \success($arguments) : \success($arguments[0]);
96
}
97
98
/**
99
 * @param mixed ...$arguments
100
 *
101
 * @return Promise
102
 */
103
function future(...$arguments): Promise
104
{
105
    return new LazyPromise(function () use ($arguments) {
106 2
        return \promise(...$arguments);
107 2
    });
108
}
109
110
/**
111
 * @param Promise $promise
112
 * @param int     $time
113
 *
114
 * @return Promise
115
 */
116
function timeout(Promise $promise, int $time): Promise
117
{
118
    return Promise\timeout($promise, $time);
119
}
120