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

Yoast / PHPUnit-Polyfills / 16579531037

28 Jul 2025 08:25PM UTC coverage: 63.368% (-32.5%) from 95.846%
16579531037

push

github

jrfnl
Merge branch '1.x' into 2.x

365 of 576 relevant lines covered (63.37%)

17.41 hits per line

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

76.92
/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 ) {
41✔
32
                return ( $actual !== null
41✔
33
                        && \is_scalar( $actual ) === false
41✔
34
                        && \is_array( $actual ) === false
41✔
35
                        && \is_object( $actual ) === false );
41✔
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 ) {
64✔
46
                $type = \gettype( $actual );
64✔
47

48
                /*
49
                 * PHP 7.2 introduced "resource (closed)".
50
                 */
51
                if ( $type === 'resource (closed)' ) {
64✔
52
                        return true;
23✔
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 );
41✔
60
                $isNotNonResource = self::isResource( $actual );
41✔
61

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

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

79
                return false;
41✔
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 ) {
57✔
96
                try {
97
                        $type = @\get_resource_type( $actual );
57✔
98

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

108
                return true;
57✔
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() {
2✔
122
                if ( \PHP_VERSION_ID >= 70008 && \PHP_VERSION_ID < 70034 ) {
2✔
123
                        return true;
×
124
                }
125

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

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

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

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

142
                return false;
2✔
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