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

Yoast / PHPUnit-Polyfills / 8548419439

04 Apr 2024 02:28AM UTC coverage: 96.232% (-1.0%) from 97.254%
8548419439

push

github

jrfnl
Tests: minor tweaks to work round PHP 8.4 deprecation

PHP 8.4 deprecates implicitly nullable parameters, i.e. typed parameter with a `null` default value, which are not explicitly declared as nullable.

The `ValueObject` fixture used in these tests used one such implicitly nullable parameters.

The tests for the `AssertObjectEquals` trait already needed two test classes to allow the method to be fully tests cross-version:
* One set which was run on PHPUnit < 9.4.0.
* One set which runs against PHP 7.0+.

As the nullability operator was introduced in PHP 7.1 and the particular test affected _does_ need to be typed to still test what it is supposed to test, I'm changing the requirements both test sets:
* The requirement for the first set of tests will now be PHPUnit < 9.4.0 AND PHP < 8.4.
* The requirement for the second set of test will now be PHP 7.1+.

This way the tests still covers the trait sufficiently.

_Note: the trait itself is not affected by the deprecation._

664 of 690 relevant lines covered (96.23%)

60.9 hits per line

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

90.0
/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 ) {
670✔
30
                return ( $actual !== null
422✔
31
                        && \is_scalar( $actual ) === false
670✔
32
                        && \is_array( $actual ) === false
670✔
33
                        && \is_object( $actual ) === false );
670✔
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 ) {
760✔
44
                $type = \gettype( $actual );
760✔
45

46
                /*
47
                 * PHP 7.2 introduced "resource (closed)".
48
                 */
49
                if ( $type === 'resource (closed)' ) {
760✔
50
                        return true;
90✔
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 );
670✔
58
                $isNotNonResource = self::isResource( $actual );
670✔
59

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

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

77
                return false;
494✔
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 ) {
295✔
94
                try {
95
                        $type = @\get_resource_type( $actual );
295✔
96

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

106
                return true;
283✔
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
         * {@internal IMPORTANT: Any changes made to this function should also be made
118
         * to the function in the "empty" version of this trait.}
119
         *
120
         * @return bool
121
         */
122
        public static function isIncompatiblePHPForLibXMLResources() {
30✔
123
                if ( \PHP_VERSION_ID >= 70008 && \PHP_VERSION_ID < 70034 ) {
30✔
124
                        return true;
4✔
125
                }
126

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

131
                if ( \PHP_VERSION_ID >= 70200 && \PHP_VERSION_ID < 70235 ) {
22✔
132
                        return true;
4✔
133
                }
134

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

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

143
                return false;
18✔
144
        }
145
}
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