Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlysystemReaderBackCompatTrait
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
4.25
0.00% covered (danger)
0.00%
0 / 1
 has
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
4.25
1<?php
2
3/**
4 * `FilesystemReader` interface v3 introduced `::directoryExists()` and `::has()`.
5 *
6 * When this trait is used with your implementation of {@see \League\Flysystem\FilesystemReader} and `league/flysystem`
7 * v3 is installed, the genuine parent methods are used, when v2 is installed the trait provides implementations.
8 *
9 * directoryExists, has
10 *
11 * v2: https://github.com/thephpleague/flysystem/blob/2.x/src/FilesystemReader.php
12 * v3: https://github.com/thephpleague/flysystem/blob/3.x/src/FilesystemReader.php
13 * Implementation: https://github.com/thephpleague/flysystem/blob/3.x/src/Filesystem.php#L46-L51
14 *
15 * `FilesystemAdapter` 3.x also introduced `::directoryExists()` so we re-use its compatibility trait,
16 * {@see FlysystemAdapterBackCompatTrait}, here.
17 *
18 * @package BrianHenryIE\FlysystemReadOnly
19 */
20
21namespace BrianHenryIE\FlysystemReadOnly;
22
23/**
24 * Back-fills `FilesystemReader::has()` and `::directoryExists()` when `league/flysystem` v2 is installed.
25 *
26 * @mixin FlysystemReaderBackCompatTraitInterface
27 *
28 * @method string normalizePath($location)
29 * @method bool fileExists(string $location)
30 */
31trait FlysystemReaderBackCompatTrait
32{
33    use FlysystemAdapterBackCompatTrait;
34
35    /**
36     * Determine whether a file or a directory exists, using the parent's implementation where there is one.
37     *
38     * @param string $location The path to check.
39     *
40     * @throws \League\Flysystem\FilesystemException
41     */
42    public function has(string $location): bool
43    {
44        /**
45         * The parent of the current class is checked, not necessarily the parent of the class which was called.
46         *
47         * @phpstan-ignore booleanAnd.leftAlwaysTrue
48         */
49        if (get_parent_class(self::class) && method_exists(get_parent_class(self::class), 'has')) {
50            /**
51             * The parent's `::has()` is not declared by v2's `FilesystemReader`, so it is not visible here.
52             *
53             * @phpstan-ignore staticMethod.notFound
54             */
55            return parent::has($location);
56        }
57
58        return $this->fileExists($this->normalizePath($location))
59               || $this->directoryExists($this->normalizePath($location));
60    }
61}