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

Yoast / PHPUnit-Polyfills / 13226989791

09 Feb 2025 03:13PM UTC coverage: 98.331%. Remained the same
13226989791

Pull #247

github

web-flow
Merge acc65568e into a4f58ea08
Pull Request #247: Composer/GH Actions: allow for PHPUnit 12.x and make the tests cross-version compatible

589 of 599 relevant lines covered (98.33%)

143.31 hits per line

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

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

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

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

66
                if ( $isNotNonResource === true ) {
449✔
67
                        try {
68
                                $resourceType = @\get_resource_type( $actual );
240✔
69
                                if ( $resourceType === 'Unknown' ) {
240✔
70
                                        return true;
240✔
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;
449✔
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 ): bool {
403✔
96
                try {
97
                        $type = @\get_resource_type( $actual );
403✔
98

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

108
                return true;
391✔
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.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(): bool {
28✔
122
                if ( \PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70134 ) {
28✔
123
                        return true;
6✔
124
                }
125

126
                if ( \PHP_VERSION_ID >= 70200 && \PHP_VERSION_ID < 70235 ) {
22✔
127
                        return true;
6✔
128
                }
129

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

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

138
                return false;
16✔
139
        }
140
}
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

© 2026 Coveralls, Inc