Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem scoping the AWS PHP SDK v3 (gmdate constant) #301

Closed
poisa opened this issue Feb 19, 2019 · 5 comments · Fixed by #341
Closed

Problem scoping the AWS PHP SDK v3 (gmdate constant) #301

poisa opened this issue Feb 19, 2019 · 5 comments · Fixed by #341
Labels
Milestone

Comments

@poisa
Copy link
Contributor

poisa commented Feb 19, 2019

Bug report

Question Answer
Box version 0.11.4
PHP version 7.1.22
Platform with version MacOS
Github Repo https://github.com/aws/aws-sdk-php
scoper.inc.php
<?php

declare(strict_types=1);

use Isolated\Symfony\Component\Finder\Finder;

return [
   // The prefix configuration. If a non null value will be used, a random prefix will be generated.
   'prefix' => 'Pastelito',

   // By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
   // directory. You can however define which files should be scoped by defining a collection of Finders in the
   // following configuration key.
   //
   // For more see: https://github.com/humbug/php-scoper#finders-and-paths
   'finders' => [
       Finder::create()->files()->in('./'),
       Finder::create()
           ->files()
           ->ignoreVCS(true)
           ->notName('/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/')
           ->exclude([
               'doc',
               'test',
               'test_old',
               'tests',
               'Tests',
               'vendor-bin',
           ])
           ->in('vendor'),
       Finder::create()->append([
           'composer.json',
       ]),
   ],

   // Whitelists a list of files. Unlike the other whitelist related features, this one is about completely leaving
   // a file untouched.
   // Paths are relative to the configuration file unless if they are already absolute
   'files-whitelist' => [
       'src/a-whitelisted-file.php',
   ],

   // When scoping PHP files, there will be scenarios where some of the code being scoped indirectly references the
   // original namespace. These will include, for example, strings or string manipulations. PHP-Scoper has limited
   // support for prefixing such strings. To circumvent that, you can define patchers to manipulate the file to your
   // heart contents.
   //
   // For more see: https://github.com/humbug/php-scoper#patchers
   'patchers' => [
       function (string $filePath, string $prefix, string $contents): string {
           $suffix = '/vendor/aws/aws-sdk-php/src/AwsClient.php';
           if (substr($filePath, -strlen($suffix)) === $suffix) {
               return str_replace(
                   'Aws\\\\{$service}\\\\Exception\\\\{$service}Exception',
                   $prefix . '\\\\Aws\\\\{$service}\\\\Exception\\\\{$service}Exception',
                   $contents
               );
           }
           return $contents;
       },
   ],

   // PHP-Scoper's goal is to make sure that all code for a project lies in a distinct PHP namespace. However, you
   // may want to share a common API between the bundled code of your PHAR and the consumer code. For example if
   // you have a PHPUnit PHAR with isolated code, you still want the PHAR to be able to understand the
   // PHPUnit\Framework\TestCase class.
   //
   // A way to achieve this is by specifying a list of classes to not prefix with the following configuration key. Note
   // that this does not work with functions or constants neither with classes belonging to the global namespace.
   //
   // Fore more see https://github.com/humbug/php-scoper#whitelist
   'whitelist' => [
       // 'PHPUnit\Framework\TestCase',   // A specific class
       // 'PHPUnit\Framework\*',          // The whole namespace
       // '*',                            // Everything
   ],

   // If `true` then the user defined constants belonging to the global namespace will not be prefixed.
   //
   // For more see https://github.com/humbug/php-scoper#constants--constants--functions-from-the-global-namespace
   'whitelist-global-constants' => true,

   // If `true` then the user defined classes belonging to the global namespace will not be prefixed.
   //
   // For more see https://github.com/humbug/php-scoper#constants--constants--functions-from-the-global-namespace
   'whitelist-global-classes' => true,

   // If `true` then the user defined functions belonging to the global namespace will not be prefixed.
   //
   // For more see https://github.com/humbug/php-scoper#constants--constants--functions-from-the-global-namespace
   'whitelist-global-functions' => true,
];

There are a couple places where php-scoper thinks the string is a namespace but it isn't.

For example, these:

const ISO8601_BASIC = 'Ymd\THis\Z';
// ...
$parsed['query']['X-Amz-Date'] = gmdate('Ymd\THis\Z', $startTimestamp);

... get turned into thse:

const ISO8601_BASIC = 'Pastelito\\Ymd\\THis\\Z';
// ...
$parsed['query']['X-Amz-Date'] = \gmdate('Pastelito\\Ymd\\THis\\Z', $startTimestamp);

The exact places where this happens are:

  1. https://github.com/aws/aws-sdk-php/blob/a9b67cbcf53eaebdc0de5b7f0c78818ae9848a15/src/Signature/SignatureV4.php#L16
  2. https://github.com/aws/aws-sdk-php/blob/master/src/Signature/SignatureV4.php#L161
@theofidry
Copy link
Member

I'm not sure it's fixable (I hope it is). I think it's a flow in https://github.com/humbug/php-scoper/blob/master/src/PhpParser/NodeVisitor/StringScalarPrefixer.php.

In any case, I recommend to fix it with a patcher meanwhile, as unless you take a look at it, I don't know when I'll be able to.

@theofidry theofidry added the bug label Feb 19, 2019
@theofidry theofidry added this to the 1.0.0 milestone Feb 19, 2019
theofidry added a commit to theofidry/php-scoper that referenced this issue Jun 10, 2019
theofidry added a commit that referenced this issue Jun 10, 2019
@poisa
Copy link
Contributor Author

poisa commented Jun 10, 2019

This is wonderful, thanks! 👍

@theofidry
Copy link
Member

theofidry commented Jun 10, 2019 via email

@kkmuffme
Copy link

kkmuffme commented Jun 5, 2024

This is broken again:

const ISO8601_BASIC = 'Ymd\THis\Z';

Possibly any string that starts with Y-?m-?d should be skipped

@theofidry
Copy link
Member

Possibly any string that starts with Y-?m-?d should be skipped

Unfortunately this is a bit tricky :/ Ymd\THis\Z could very well be a valid fully qualified class name.

It won't this this exact case, but I would like to find a way to say within to scope or not a string: #1032

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants