• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / PHPUnit-Polyfills / 10743946974

06 Sep 2024 07:13PM UTC coverage: 96.423% (+0.6%) from 95.846%
10743946974

push

github

web-flow
Merge pull request #192 from Yoast/feature/3.x/drop-support-for-php-7.0

3.0 | Drop support for PHP < 7.0

28 of 29 new or added lines in 1 file covered. (96.55%)

2 existing lines in 2 files now uncovered.

620 of 643 relevant lines covered (96.42%)

124.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.18
/src/Helpers/ResourceHelper.php
1
<?php
2

3
namespace Yoast\PHPUnitPolyfills\Helpers;
4

5
use Exception;
6
use TypeError;
7

8
/**
9
 * Helper functions for working with the resource type.
10
 *
11
 * ---------------------------------------------------------------------------------------------
12
 * This class is only intended for internal use by PHPUnit Polyfills and is not part of the public API.
13
 * This also means that it has no promise of backward compatibility.
14
 *
15
 * End-user should use the {@see \Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource()} trait instead.
16
 * ---------------------------------------------------------------------------------------------
17
 *
18
 * @internal
19
 */
20
final class ResourceHelper {
21

22
        /**
23
         * Determines whether a variable represents a resource, either open or closed.
24
         *
25
         * @param mixed $actual The variable to test.
26
         *
27
         * @return bool
28
         */
29
        public static function isResource( $actual ) {
697✔
30
                return ( $actual !== null
697✔
31
                        && \is_scalar( $actual ) === false
697✔
32
                        && \is_array( $actual ) === false
697✔
33
                        && \is_object( $actual ) === false );
697✔
34
        }
35

36
        /**
37
         * Determines whether a variable represents a closed resource.
38
         *
39
         * @param mixed $actual The variable to test.
40
         *
41
         * @return bool
42
         */
43
        public static function isClosedResource( $actual ) {
906✔
44
                $type = \gettype( $actual );
906✔
45

46
                /*
47
                 * PHP 7.2 introduced "resource (closed)".
48
                 */
49
                if ( $type === 'resource (closed)' ) {
906✔
50
                        return true;
209✔
51
                }
52

53
                /*
54
                 * If gettype did not work, attempt to determine whether this is
55
                 * a closed resource in another way.
56
                 */
57
                $isResource       = \is_resource( $actual );
697✔
58
                $isNotNonResource = self::isResource( $actual );
697✔
59

60
                if ( $isResource === false && $isNotNonResource === true ) {
697✔
61
                        return true;
109✔
62
                }
63

64
                if ( $isNotNonResource === true ) {
588✔
65
                        try {
66
                                $resourceType = @\get_resource_type( $actual );
322✔
67
                                if ( $resourceType === 'Unknown' ) {
322✔
68
                                        return true;
322✔
69
                                }
UNCOV
70
                        } catch ( TypeError $e ) {
×
71
                                // Ignore. Not a resource.
72
                        } catch ( Exception $e ) {
×
73
                                // Ignore. Not a resource.
74
                        }
75
                }
76

77
                return false;
588✔
78
        }
79

80
        /**
81
         * Helper function to determine whether the open/closed state of a resource is reliable.
82
         *
83
         * Due to some bugs in PHP itself, the "is closed resource" determination
84
         * cannot always be done reliably.
85
         *
86
         * This function can determine whether or not the current value in combination with
87
         * the current PHP version on which the test is being run is affected by this.
88
         *
89
         * @param mixed $actual The variable to test.
90
         *
91
         * @return bool
92
         */
93
        public static function isResourceStateReliable( $actual ) {
371✔
94
                try {
95
                        $type = @\get_resource_type( $actual );
371✔
96

97
                        if ( $type === 'xml' && self::isIncompatiblePHPForLibXMLResources() === true ) {
263✔
98
                                return false;
263✔
99
                        }
100
                } catch ( TypeError $e ) {
108✔
101
                        // Ignore. Not a resource.
102
                } catch ( Exception $e ) {
×
103
                        // Ignore. Not a resource.
104
                }
105

106
                return true;
353✔
107
        }
108

109
        /**
110
         * Check if the PHP version is one of a known set of PHP versions
111
         * containing a libxml version which does not report on closed resources
112
         * correctly.
113
         *
114
         * Version ranges based on {@link https://3v4l.org/tc4fE}.
115
         * 7.0.8 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.21, 7.4.0 - 7.4.9
116
         *
117
         * @return bool
118
         */
119
        public static function isIncompatiblePHPForLibXMLResources() {
34✔
120
                if ( \PHP_VERSION_ID >= 70008 && \PHP_VERSION_ID < 70034 ) {
34✔
121
                        return true;
4✔
122
                }
123

124
                if ( \PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70134 ) {
30✔
125
                        return true;
6✔
126
                }
127

128
                if ( \PHP_VERSION_ID >= 70200 && \PHP_VERSION_ID < 70235 ) {
24✔
129
                        return true;
8✔
130
                }
131

132
                if ( \PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70322 ) {
16✔
133
                        return true;
×
134
                }
135

136
                if ( \PHP_VERSION_ID >= 70400 && \PHP_VERSION_ID < 70410 ) {
16✔
137
                        return true;
×
138
                }
139

140
                return false;
16✔
141
        }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc