Skip to content

Commit 58962a0

Browse files
authored
Fix PHPStan static analysis errors (php-curl-class#929)
* Increase PHPStan memory limit on local development * Add emjois to test warnings and test errors * Fix PHPStan static analysis errors Error messages: ------ ------------------------------------------------------------------------------------------------------ Line src/Curl/Curl.php ------ ------------------------------------------------------------------------------------------------------ 123 Cannot unset property Curl\Curl::$curlErrorCodeConstant because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 124 Cannot unset property Curl\Curl::$curlErrorCodeConstants because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 125 Cannot unset property Curl\Curl::$curlOptionCodeConstants because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 126 Cannot unset property Curl\Curl::$effectiveUrl because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 127 Cannot unset property Curl\Curl::$rfc2616 because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 128 Cannot unset property Curl\Curl::$rfc6265 because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 129 Cannot unset property Curl\Curl::$totalTime because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 564 Cannot unset property Curl\Curl::$curlErrorCodeConstant because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 565 Cannot unset property Curl\Curl::$effectiveUrl because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty 566 Cannot unset property Curl\Curl::$totalTime because it might have hooks in a subclass. 🪪 unset.possiblyHookedProperty ------ ------------------------------------------------------------------------------------------------------
1 parent 7dc4b9c commit 58962a0

8 files changed

+64
-41
lines changed

src/Curl/Curl.php

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ class Curl extends BaseCurl
9191

9292
public $curlErrorCodeConstant;
9393
public $curlErrorCodeConstants;
94-
public $curlOptionCodeConstants;
95-
public $effectiveUrl;
96-
public $rfc2616;
97-
public $rfc6265;
98-
public $totalTime;
9994

10095
private static $deferredProperties = [
10196
'curlErrorCodeConstant',
@@ -106,6 +101,7 @@ class Curl extends BaseCurl
106101
'rfc6265',
107102
'totalTime',
108103
];
104+
private array $deferredValues = [];
109105

110106
/**
111107
* Construct
@@ -120,13 +116,13 @@ public function __construct($base_url = null, $options = [])
120116
throw new \ErrorException('cURL library is not loaded');
121117
}
122118

123-
unset($this->curlErrorCodeConstant);
124-
unset($this->curlErrorCodeConstants);
125-
unset($this->curlOptionCodeConstants);
126-
unset($this->effectiveUrl);
127-
unset($this->rfc2616);
128-
unset($this->rfc6265);
129-
unset($this->totalTime);
119+
unset($this->deferredValues['curlErrorCodeConstant']);
120+
unset($this->deferredValues['curlErrorCodeConstants']);
121+
unset($this->deferredValues['curlOptionCodeConstants']);
122+
unset($this->deferredValues['effectiveUrl']);
123+
unset($this->deferredValues['rfc2616']);
124+
unset($this->deferredValues['rfc6265']);
125+
unset($this->deferredValues['totalTime']);
130126

131127
$this->curl = curl_init();
132128
$this->initialize($base_url, $options);
@@ -521,7 +517,7 @@ public function exec($ch = null)
521517
if ($this->curlError) {
522518
$curl_error_message = curl_strerror($this->curlErrorCode);
523519

524-
if ($this->curlErrorCodeConstant !== '') {
520+
if (isset($this->curlErrorCodeConstant)) {
525521
$curl_error_message .= ' (' . $this->curlErrorCodeConstant . ')';
526522
}
527523

@@ -561,9 +557,9 @@ public function exec($ch = null)
561557
$this->errorMessage = $this->curlError ? $this->curlErrorMessage : $this->httpErrorMessage;
562558

563559
// Reset select deferred properties so that they may be recalculated.
564-
unset($this->curlErrorCodeConstant);
565-
unset($this->effectiveUrl);
566-
unset($this->totalTime);
560+
unset($this->deferredValues['curlErrorCodeConstant']);
561+
unset($this->deferredValues['effectiveUrl']);
562+
unset($this->deferredValues['totalTime']);
567563

568564
// Reset content-length header possibly set from a PUT or SEARCH request.
569565
$this->unsetHeader('Content-Length');
@@ -1653,14 +1649,32 @@ public function __destruct()
16531649

16541650
public function __get($name)
16551651
{
1656-
$return = null;
1657-
if (
1658-
in_array($name, self::$deferredProperties, true) &&
1659-
is_callable([$this, $getter = 'get' . ucfirst($name)])
1660-
) {
1661-
$return = $this->$name = $this->$getter();
1652+
if (in_array($name, self::$deferredProperties, true)) {
1653+
if (isset($this->deferredValues[$name])) {
1654+
return $this->deferredValues[$name];
1655+
} elseif (is_callable([$this, $getter = 'get' . ucfirst($name)])) {
1656+
$this->deferredValues[$name] = $this->$getter();
1657+
return $this->deferredValues[$name];
1658+
}
1659+
}
1660+
1661+
return null;
1662+
}
1663+
1664+
public function __isset($name)
1665+
{
1666+
if (in_array($name, self::$deferredProperties, true)) {
1667+
if (isset($this->deferredValues[$name])) {
1668+
return true;
1669+
} elseif (is_callable([$this, $getter = 'get' . ucfirst($name)])) {
1670+
$this->deferredValues[$name] = $this->$getter();
1671+
return true;
1672+
} else {
1673+
return false;
1674+
}
16621675
}
1663-
return $return;
1676+
1677+
return isset($this->$name);
16641678
}
16651679

16661680
/**
@@ -1685,7 +1699,7 @@ function ($key) {
16851699
*/
16861700
private function getCurlErrorCodeConstant()
16871701
{
1688-
$curl_const_by_code = $this->curlErrorCodeConstants;
1702+
$curl_const_by_code = $this->curlErrorCodeConstants ?? [];
16891703
if (isset($curl_const_by_code[$this->curlErrorCode])) {
16901704
return $curl_const_by_code[$this->curlErrorCode];
16911705
}

tests/display_errors.inc.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ if [[ "${error_count}" -ge 1 ]]; then
66
for value in "${errors[@]}"; do
77
((iter++))
88
echo -e "\nError ${iter} of ${error_count}:"
9-
echo "${value}" | perl -pe 's/^(.*)$/\t\1/'
9+
echo "${value}" | perl -pe 's/^(.*)$/\t\1/'
1010
done
1111
fi

tests/run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if [[ "${CI}" == "true" ]]; then
88
composer self-update
99
composer install --prefer-source --no-interaction
1010
if [[ "${?}" -ne 0 ]]; then
11-
echo "Error: composer install failed"
11+
echo "Error: composer install failed"
1212
errors+=("composer install failed")
1313
fi
1414
fi
@@ -43,5 +43,5 @@ source "display_errors.inc.sh"
4343
if [[ "${CI_PHP_FUTURE_RELEASE}" != "true" ]]; then
4444
exit "${#errors[@]}"
4545
elif [[ "${#errors[@]}" -ne 0 ]]; then
46-
echo "One or more tests failed, but allowed as the CI_PHP_FUTURE_RELEASE flag is on for PHP version ${CI_PHP_VERSION}."
46+
echo "⚠️ One or more tests failed, but allowed as the CI_PHP_FUTURE_RELEASE flag is on for PHP version ${CI_PHP_VERSION}."
4747
fi

tests/run_coding_standards_check.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ pushd ..
1212
crlf_file=$(find . -type "f" -iname "*.php" ! -path "*/vendor/*" -exec grep --color=always --files-with-matches $'\r' {} \;)
1313
if [[ ! -z "${crlf_file}" ]]; then
1414
result="$(echo "${crlf_file}" | perl -pe 's/(.*)/CRLF line terminators found in \1/')"
15-
echo "${result}"
15+
echo "${result}"
1616
errors+=("${result}")
1717
fi
1818

1919
# Enforce indentation character consistency in php files.
2020
tab_char=$(find . -type "f" -iname "*.php" ! -path "*/vendor/*" -exec grep --color=always --line-number -H --perl-regexp "\t" {} \;)
2121
if [[ ! -z "${tab_char}" ]]; then
2222
result="$(echo -e "${tab_char}" | perl -pe 's/^(.*)$/Tab character found in \1/')"
23-
echo "${result}"
23+
echo "${result}"
2424
errors+=("${result}")
2525
fi
2626

@@ -68,31 +68,31 @@ EOF
6868
export -f "find_invalid_indentation"
6969
invalid_indentation=$(find . -type "f" -iname "*.php" ! -path "*/vendor/*" -exec bash -c 'find_invalid_indentation "{}"' \;)
7070
if [[ ! -z "${invalid_indentation}" ]]; then
71-
echo "${invalid_indentation}"
71+
echo "${invalid_indentation}"
7272
errors+=("${invalid_indentation}")
7373
fi
7474

7575
# Prohibit trailing whitespace in php files.
7676
trailing_whitespace=$(find . -type "f" -iname "*.php" ! -path "*/vendor/*" -exec grep --color=always --extended-regexp --line-number -H " +$" {} \;)
7777
if [[ ! -z "${trailing_whitespace}" ]]; then
7878
result="$(echo -e "${trailing_whitespace}" | perl -pe 's/^(.*)$/Trailing whitespace found in \1/')"
79-
echo "${result}"
79+
echo "${result}"
8080
errors+=("${result}")
8181
fi
8282

8383
# Require identical comparison operators (===, not ==) in php files.
8484
equal=$(find . -type "f" -iname "*.php" ! -path "*/vendor/*" -exec grep --color=always --extended-regexp --line-number -H "[^!=]==[^=]" {} \;)
8585
if [[ ! -z "${equal}" ]]; then
8686
result="$(echo -e "${equal}" | perl -pe 's/^(.*)$/Non-identical comparison operator found in \1/')"
87-
echo "${result}"
87+
echo "${result}"
8888
errors+=("${result}")
8989
fi
9090

9191
# Require both braces on else statement line; "} else {" and not "}\nelse {".
9292
elses=$(find . -type "f" -iname "*.php" ! -path "*/vendor/*" -exec grep --color=always --line-number -H --perl-regexp '^(\s+)?else(\s+)?{' {} \;)
9393
if [[ ! -z "${elses}" ]]; then
9494
result="$(echo -e "${elses}" | perl -pe 's/^(.*)$/Found newline before "else" statement in \1/')"
95-
echo "${result}"
95+
echo "${result}"
9696
errors+=("${result}")
9797
fi
9898

@@ -114,7 +114,7 @@ fi
114114
-s \
115115
.
116116
if [[ "${?}" -ne 0 ]]; then
117-
echo "Error: found PHP_CodeSniffer coding standard violation(s)"
117+
echo "Error: found PHP_CodeSniffer coding standard violation(s)"
118118
errors+=("found PHP_CodeSniffer coding standard violation(s)")
119119
fi
120120

@@ -127,7 +127,7 @@ if [[ $(echo "${CI_PHP_VERSION} < 8.4" | bc -l) -eq 1 ]]; then
127127
vendor/bin/php-cs-fixer --version
128128
vendor/bin/php-cs-fixer fix --ansi --config="tests/.php-cs-fixer.php" --diff --dry-run
129129
if [[ "${?}" -ne 0 ]]; then
130-
echo "Error: found PHP-CS-Fixer coding standard violation(s)"
130+
echo "Error: found PHP-CS-Fixer coding standard violation(s)"
131131
errors+=("found PHP-CS-Fixer coding standard violation(s)")
132132
fi
133133

tests/run_phpunit.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fi
123123
--configuration "phpunit.xml" \
124124
${phpunit_args}
125125
if [[ "${?}" -ne 0 ]]; then
126-
echo "Error: phpunit command failed"
126+
echo "Error: phpunit command failed"
127127
errors+=("phpunit command failed")
128128
fi
129129

tests/run_static_analysis_check_phpstan.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ pushd ..
1111
set -x
1212

1313
if [[ $(echo "${CI_PHP_VERSION} >= 7.4" | bc -l) -eq 1 ]]; then
14-
vendor/bin/phpstan analyse --ansi --configuration="tests/phpstan.neon" .
14+
15+
phpstan_args=(--ansi --configuration="tests/phpstan.neon")
16+
17+
# Increase memory limit on local development.
18+
if [[ "${CI}" != "true" ]]; then
19+
phpstan_args+=(--memory-limit=256M)
20+
fi
21+
22+
vendor/bin/phpstan --version
23+
vendor/bin/phpstan analyse "${phpstan_args[@]}" .
1524
if [[ "${?}" -ne 0 ]]; then
16-
echo "Error: phpstan static analysis check failed"
25+
echo "Error: phpstan static analysis check failed"
1726
errors+=("phpstan static analysis check failed")
1827
fi
1928
else
20-
echo "Skipped running phpstan check"
29+
echo "⚠️ Skipped running phpstan check"
2130
fi
2231

2332
popd

tests/run_static_analysis_check_psalm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ else
1919
fi
2020

2121
if [[ "${?}" -ne 0 ]]; then
22-
echo "Error: psalm static analysis check failed"
22+
echo "Error: psalm static analysis check failed"
2323
errors+=("psalm static analysis check failed")
2424
fi
2525

tests/run_syntax_check.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pushd ..
99
# Check syntax in php files. Use `xargs' over `find -exec' as xargs exits with a value of 1 when any command errors.
1010
find . -type "f" -iname "*.php" ! -path "*/vendor/*" | xargs -L "1" php -l
1111
if [[ "${?}" -ne 0 ]]; then
12-
echo "Error: php syntax checks failed"
12+
echo "Error: php syntax checks failed"
1313
errors+=("php syntax checks failed")
1414
fi
1515

0 commit comments

Comments
 (0)