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

Yoast / PHPUnit-Polyfills / 19485817145

19 Nov 2025 12:54AM UTC coverage: 95.833% (-0.6%) from 96.408%
19485817145

push

github

web-flow
Merge pull request #269 from Yoast/dependabot/github_actions/1.x/DavidAnson/markdownlint-cli2-action-21

GH Actions: Bump DavidAnson/markdownlint-cli2-action from 20 to 21

667 of 696 relevant lines covered (95.83%)

63.18 hits per line

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

87.5
/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
 * @since 1.0.0
21
 */
22
final class ResourceHelper {
23

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

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

48
                /*
49
                 * PHP 7.2 introduced "resource (closed)".
50
                 */
51
                if ( $type === 'resource (closed)' ) {
724✔
52
                        return true;
67✔
53
                }
54

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

62
                if ( $isResource === false && $isNotNonResource === true ) {
657✔
63
                        return true;
183✔
64
                }
65

66
                if ( $isNotNonResource === true ) {
474✔
67
                        try {
68
                                $resourceType = @\get_resource_type( $actual );
246✔
69
                                if ( $resourceType === 'Unknown' ) {
246✔
70
                                        return true;
146✔
71
                                }
72
                        } catch ( TypeError $e ) {
100✔
73
                                // Ignore. Not a resource.
74
                        } catch ( Exception $e ) {
×
75
                                // Ignore. Not a resource.
76
                        }
77
                }
100✔
78

79
                return false;
474✔
80
        }
81

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

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

108
                return true;
292✔
109
        }
110

111
        /**
112
         * Check if the PHP version is one of a known set of PHP versions
113
         * containing a libxml version which does not report on closed resources
114
         * correctly.
115
         *
116
         * Version ranges based on {@link https://3v4l.org/tc4fE}.
117
         * 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
118
         *
119
         * @return bool
120
         */
121
        public static function isIncompatiblePHPForLibXMLResources() {
28✔
122
                if ( \PHP_VERSION_ID >= 70008 && \PHP_VERSION_ID < 70034 ) {
28✔
123
                        return true;
4✔
124
                }
125

126
                if ( \PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70134 ) {
24✔
127
                        return true;
4✔
128
                }
129

130
                if ( \PHP_VERSION_ID >= 70200 && \PHP_VERSION_ID < 70235 ) {
20✔
131
                        return true;
×
132
                }
133

134
                if ( \PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70322 ) {
20✔
135
                        return true;
×
136
                }
137

138
                if ( \PHP_VERSION_ID >= 70400 && \PHP_VERSION_ID < 70410 ) {
20✔
139
                        return true;
×
140
                }
141

142
                return false;
20✔
143
        }
144
}
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