bshaffer/google-cloud-new-surface-fixer

There is no license information available for the latest version (dev-main) of this package.

dev-main 2024-01-12 02:35 UTC

This package is auto-updated.

Last update: 2024-04-12 03:08:58 UTC


README

This repo provides a Fixer, to be used with PHP CS Fixer, which will automatically** upgrade your code to the new Google Cloud PHP client surface.

** this is an alpha tool and NOT recommended to use without thorough testing! It is also not guanteed by Google in any way.

Installation

Install the fixer:

composer require --dev "bshaffer/google-cloud-new-surface-fixer"

NOTE:This package relies on "friendsofphp/php-cs-fixer" v3 as a dependency, but feel free to install your own compatible version as well:

Running the fixer

First, create a .php-cs-fixer.google.php in your project which will be configured to use the custom fixer:

<?php
// .php-cs-fixer.google.php

// The fixer MUST autoload google/cloud classes. This line is only necessary if
// "php-cs-fixer" was installed in a different composer.json, e.g. with
// "composer global require".
require __DIR__ . '/vendor/autoload.php';

// configure the fixer to run with the new surface fixer
return (new PhpCsFixer\Config())
    ->registerCustomFixers([
        new Google\Cloud\Tools\NewSurfaceFixer(),
    ])
    ->setRules([
        'GoogleCloud/new_surface_fixer' => true,
    ])
;

Run this fixer with the following command:

export DIR=examples
vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.google.php --dry-run --diff $DIR

You should get an output similar to this

--- examples/legacy_optional_args.php
+++ examples/legacy_optional_args.php
@@ -2,10 +2,12 @@

 namespace Google\Cloud\Samples\Dlp;

-use Google\Cloud\Dlp\V2\DlpServiceClient;
+use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
+use Google\Cloud\Dlp\V2\CreateDlpJobRequest;
 use Google\Cloud\Dlp\V2\InspectConfig;
 use Google\Cloud\Dlp\V2\InspectJobConfig;
 use Google\Cloud\Dlp\V2\Likelihood;
+use Google\Cloud\Dlp\V2\ListInfoTypesRequest;
 use Google\Cloud\Dlp\V2\StorageConfig;

 // Instantiate a client.
@@ -12,14 +14,20 @@
 $dlp = new DlpServiceClient();

 // optional args array (variable)
-$infoTypes = $dlp->listInfoTypes($parent);
+$request = (new ListInfoTypesRequest());
+$infoTypes = $dlp->listInfoTypes($request);

 // optional args array (inline array)
-$job = $dlp->createDlpJob($parent, ['jobId' => 'abc', 'locationId' => 'def']);
+$request2 = (new CreateDlpJobRequest())
+    ->setParent($parent)
+    ->setJobId('abc')
+    ->setLocationId('def');
+$job = $dlp->createDlpJob($request2);

 // optional args array (inline with nested arrays)
-$job = $dlp->createDlpJob($parent, [
-    'inspectJob' => new InspectJobConfig([
+$request3 = (new CreateDlpJobRequest())
+    ->setParent($parent)
+    ->setInspectJob(new InspectJobConfig([
         'inspect_config' => (new InspectConfig())
             ->setMinLikelihood(likelihood::LIKELIHOOD_UNSPECIFIED)
             ->setLimits($limits)
@@ -28,5 +36,5 @@
         'storage_config' => (new StorageConfig())
             ->setCloudStorageOptions(($cloudStorageOptions))
             ->setTimespanConfig($timespanConfig),
-    ])
-]);
+    ]));
+$job = $dlp->createDlpJob($request3);

      ----------- end diff -----------