Completed
Pull Request — master (#3)
by Amine
02:42
created

run.php ➔ main()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 0
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 112.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Returns the list of scripts to compare.
5
 *
6
 * @return array
7
 */
8
function scripts() {
9
    return [
10
        __DIR__ . '/scripts/imperative.php',
11
        __DIR__ . '/scripts/functional.php',
12
        __DIR__ . '/scripts/object-oriented.php',
13
    ];
14
}
15
16
/**
17
 * Returs the list of tests to run.
18
 *
19
 * @return array
20
 */
21
function tests() {
22
    static $tests = null;
23
    if (null == $tests) {
24
        $tests = [
25
            (object) [
26
                'input' => __DIR__ . '/tests/1K-words.input.txt',
27
                'output' => trim(file_get_contents(__DIR__ . '/tests/1K-words.output.txt'))
28
            ],
29
            (object) [
30
                'input' => __DIR__ . '/tests/10k-words.input.txt',
31
                'output' => trim(file_get_contents(__DIR__ . '/tests/10k-words.output.txt'))
32
            ],
33
            (object) [
34
                'input' => __DIR__ . '/tests/100k-words.input.txt',
35
                'output' => trim(file_get_contents(__DIR__ . '/tests/100k-words.output.txt'))
36
            ],
37
        ];
38
    }
39
    return $tests;
40
}
41
42
/**
43
 * Runs the scripts with the tests and shows the results.
44
 *
45
 * @return void
46
 */
47
function main() {
48
    foreach (scripts() as $path) {
49
        echo nameOf($path), ":\n";
50
        foreach (tests() as $test) {
0 ignored issues
show
Bug introduced by
The expression tests() of type array<integer,?>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
51
            echo "  ", nameOf($test->input), ": ";
52
            if (!passes($path, $test)) {
53
                echo "Failed!\n";
54
            } else {
55
                echo averageTime(3, $path, $test), "\n";
56
            }
57
        }
58
    }
59
}
60
61
/**
62
 * Gets the name of a file without extension from its path.
63
 *
64
 * @param  string $path
65
 * @return string
66
 */
67
function nameOf($path) {
68
    $name = substr($path, strrpos($path, '/') + 1);
69
    return substr($name, 0, strpos($name, '.'));
70
}
71
72
/**
73
 * Runs the given script `$n` times and returns the average running time.
74
 *
75
 * @param  int $n
76
 * @param  string $path
77
 * @param  object $test
78
 * @return int
79
 */
80
function averageTime($n, $path, $test) {
81
    $s = 0;
82
    for ($i=0; $i < $n; $i++) {
83
        $s =+ execute($path, $test->input);
84
    }
85
    return $s / $n;
86
}
87
88
/**
89
 * Checks if running a PHP script with some input returns the expected output.
90
 *
91
 * @param  string $path
92
 * @param  array $test
93
 * @return bool
94
 */
95
function passes($path, $test) {
96
    return $test->output == trim(shell_exec("php {$path} < \"{$test->input}\""));
97
}
98
99
/**
100
 * Runs a PHP file with specific standard input
101
 * and returns its running time in miliseconds.
102
 *
103
 * @param  string $path
104
 * @return array
105
 */
106
function execute($path, $input) {
107
    $start = microtime(true);
108
    shell_exec("php {$path} < \"{$input}\"");
109
    return 1000 * (microtime(true) - $start);
110
}
111
112
main();
113