File: check-scoped-phar.php

package info (click to toggle)
phpunit 12.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,180 kB
  • sloc: php: 102,523; xml: 2,166; makefile: 107; sh: 82
file content (63 lines) | stat: -rwxr-xr-x 1,575 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env php
<?php declare(strict_types=1);

/*
 * This file is part of PHPUnit.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
if (!isset($argv[1]) || !\file_exists($argv[1])) {
    \fwrite(
        \STDERR,
        \sprintf(
            '%s /path/to/phpunit-*.phar' . \PHP_EOL,
            $argv[0]
        )
    );

    exit(1);
}

if (!str_contains_between(\file_get_contents($argv[1]), 'PHPUnitPHAR', '$classes = [', '];') &&
    !str_contains_between(\file_get_contents($argv[1]), 'phpunitphar', '$classes = [', '];')) {
    \fwrite(\STDERR, 'PHAR is not scoped' . \PHP_EOL);

    exit(1);
}

\fwrite(\STDOUT, 'PHAR is scoped' . \PHP_EOL);

/**
 * @param non-empty-string $haystack
 * @param non-empty-string $needle
 * @param non-empty-string $start
 * @param non-empty-string $end
 */
function str_contains_between(string $haystack, string $needle, string $start, string $end): bool
{
    $startLen  = \strlen($start);
    $needleLen = \strlen($needle);
    $offset    = 0;

    while (($sPos = \strpos($haystack, $start, $offset)) !== false) {
        $afterStart = $sPos + $startLen;
        $ePos       = \strpos($haystack, $end, $afterStart);

        if ($ePos === false) {
            break;
        }

        $nPos = \strpos($haystack, $needle, $afterStart);

        if ($nPos !== false && $nPos + $needleLen <= $ePos) {
            return true;
        }

        $offset = $sPos + 1;
    }

    return false;
}