Skip to content

Commit

Permalink
Emit deprecation warning (#2547)
Browse files Browse the repository at this point in the history
Emits a deprecation warning for users running on versions of PHP which will be deprecated on August 15th, 2023
  • Loading branch information
SamRemis authored Feb 17, 2023
1 parent 43ee875 commit ceb929c
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 160 deletions.
7 changes: 7 additions & 0 deletions .changes/nextrelease/emit-deprecation-warning.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "feature",
"category": "",
"description": "Emit a warning to users on PHP versions 7.2.4 and below that we are ending support for this language version on 08/15/2023"
}
]
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
AWS_ACCESS_KEY_ID: foo
AWS_SECRET_ACCESS_KEY: bar
AWS_CSM_ENABLED: false
AWS_SUPPRESS_PHP_DEPRECATION_WARNING: true
steps:
#sets up the correct version of PHP with necessary config options
- name: Setup PHP with Xdebug
Expand Down
23 changes: 23 additions & 0 deletions src/AwsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ public function __construct(array $args)
$this->addRecursionDetection();
$this->addRequestBuilder();

if (!$config['suppress_php_deprecation_warning']) {
$this->emitDeprecationWarning();
}

if (isset($args['with_resolved'])) {
$args['with_resolved']($config);
}
Expand Down Expand Up @@ -588,6 +592,25 @@ protected function isUseEndpointV2()
return $this->endpointProvider instanceof EndpointProviderV2;
}

public static function emitDeprecationWarning() {
$phpVersion = PHP_VERSION_ID;
if ($phpVersion < 70205) {
$phpVersionString = phpversion();
trigger_error(
"This installation of the SDK is using PHP version"
. " {$phpVersionString}, which will be deprecated on August"
. " 15th, 2023. Please upgrade your PHP version to a minimum of"
. " 7.2.5 before then to continue receiving updates to the AWS"
. " SDK for PHP. To disable this warning, set"
. " suppress_php_deprecation_warning to true on the client constructor"
. " or set the environment variable AWS_SUPPRESS_PHP_DEPRECATION_WARNING"
. " to true.",
E_USER_DEPRECATED
);
}
}


/**
* Returns a service model and doc model with any necessary changes
* applied.
Expand Down
22 changes: 22 additions & 0 deletions src/ClientResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ class ClientResolver
'doc' => 'Set to false to disable checking for shared aws config files usually located in \'~/.aws/config\' and \'~/.aws/credentials\'. This will be ignored if you set the \'profile\' setting.',
'default' => true,
],
'suppress_php_deprecation_warning' => [
'type' => 'value',
'valid' => ['bool'],
'doc' => 'Set to false to disable the deprecation warning of PHP versions 7.2.4 and below',
'default' => false,
'fn' => [__CLASS__, '_apply_suppress_php_deprecation_warning']
],
];

/**
Expand Down Expand Up @@ -934,6 +941,21 @@ public static function _apply_idempotency_auto_fill(
}
}

public static function _apply_suppress_php_deprecation_warning($suppressWarning, array &$args) {
if ($suppressWarning) {
$args['suppress_php_deprecation_warning'] = true;
} elseif (!empty($_ENV["AWS_SUPPRESS_PHP_DEPRECATION_WARNING"])) {
$args['suppress_php_deprecation_warning'] =
$_ENV["AWS_SUPPRESS_PHP_DEPRECATION_WARNING"];
} elseif (!empty($_SERVER["AWS_SUPPRESS_PHP_DEPRECATION_WARNING"])) {
$args['suppress_php_deprecation_warning'] =
$_SERVER["AWS_SUPPRESS_PHP_DEPRECATION_WARNING"];
} elseif (!empty(getenv("AWS_SUPPRESS_PHP_DEPRECATION_WARNING"))) {
$args['suppress_php_deprecation_warning']
= getenv("AWS_SUPPRESS_PHP_DEPRECATION_WARNING");
}
}

public static function _default_endpoint_provider(array $args)
{
$service = isset($args['api']) ? $args['api'] : null;
Expand Down
78 changes: 78 additions & 0 deletions tests/AwsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Aws\Signature\SignatureV4;
use Aws\Sts\StsClient;
use Aws\WrappedHttpHandler;
use Exception;
use GuzzleHttp\Promise\RejectedPromise;
use Psr\Http\Message\RequestInterface;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
Expand Down Expand Up @@ -598,4 +599,81 @@ private function createClient(array $service = [], array $config = [])
'version' => 'latest'
]);
}

public function testThrowsDeprecationWarning() {
$storeEnvVariable = getenv('AWS_SUPPRESS_PHP_DEPRECATION_WARNING');
$storeEnvArrayVariable = isset($_ENV['AWS_SUPPRESS_PHP_DEPRECATION_WARNING']) ? $_ENV['AWS_SUPPRESS_PHP_DEPRECATION_WARNING'] : '';
$storeServerArrayVariable = isset($_SERVER['AWS_SUPPRESS_PHP_DEPRECATION_WARNING']) ? $_SERVER['AWS_SUPPRESS_PHP_DEPRECATION_WARNING'] : '';
putenv('AWS_SUPPRESS_PHP_DEPRECATION_WARNING');
unset($_ENV['AWS_SUPPRESS_PHP_DEPRECATION_WARNING']);
unset($_SERVER['AWS_SUPPRESS_PHP_DEPRECATION_WARNING']);
$expectsDeprecation = PHP_VERSION_ID < 70205;
if ($expectsDeprecation) {
try {
$this->expectDeprecation();
$this->expectDeprecationMessage("This installation of the SDK is using PHP version");
$client = new StsClient([
'region' => 'us-west-2',
'version' => 'latest'
]);
$this->fail("This test should have thrown the deprecation");
} finally {
putenv("AWS_SUPPRESS_PHP_DEPRECATION_WARNING={$storeEnvVariable}");
}
} else {
$client = new StsClient([
'region' => 'us-west-2',
'version' => 'latest'
]);
$this->assertTrue(true);
}
putenv("AWS_SUPPRESS_PHP_DEPRECATION_WARNING={$storeEnvVariable}");
if (!empty($storeEnvArrayVariable)) {
$_ENV['AWS_SUPPRESS_PHP_DEPRECATION_WARNING'] = $storeEnvArrayVariable;
}
if (!empty($storeServerArrayVariable)) {
$_SERVER['AWS_SUPPRESS_PHP_DEPRECATION_WARNING'] = $storeServerArrayVariable;
}
}

public function testCanDisableWarningWithClientConfig() {
$storeEnvVariable = getenv('AWS_SUPPRESS_PHP_DEPRECATION_WARNING');
putenv('AWS_SUPPRESS_PHP_DEPRECATION_WARNING');
$expectsDeprecation = PHP_VERSION_ID < 70205;
if ($expectsDeprecation) {
try {
$client = new StsClient([
'region' => 'us-west-2',
'version' => 'latest',
'suppress_php_deprecation_warning' => true
]);
} catch (Exception $exception) {
$this->fail("This test should not have thrown the deprecation");
}
} else {
putenv("AWS_SUPPRESS_PHP_DEPRECATION_WARNING={$storeEnvVariable}");
$this->markTestSkipped();
}
putenv("AWS_SUPPRESS_PHP_DEPRECATION_WARNING={$storeEnvVariable}");
}

public function testCanDisableWarningWithEnvVar() {
$storeEnvVariable = getenv('AWS_SUPPRESS_PHP_DEPRECATION_WARNING');
putenv('AWS_SUPPRESS_PHP_DEPRECATION_WARNING=true');
$expectsDeprecation = PHP_VERSION_ID < 70205;
if ($expectsDeprecation) {
try {
$client = new StsClient([
'region' => 'us-west-2',
'version' => 'latest'
]);
} catch (Exception $exception) {
$this->fail("This test should not have thrown the deprecation");
}
} else {
putenv("AWS_SUPPRESS_PHP_DEPRECATION_WARNING={$storeEnvVariable}");
$this->markTestSkipped();
}
putenv("AWS_SUPPRESS_PHP_DEPRECATION_WARNING={$storeEnvVariable}");
}
}
Loading

0 comments on commit ceb929c

Please sign in to comment.