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

chore: add phpstan static analysis #5931

Merged
merged 29 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
71b8f7c
WIP
bshaffer Jan 4, 2023
57577fc
WIP
bshaffer Feb 5, 2023
3f4a71a
addressing php stan issues
bshaffer Feb 6, 2023
3b18cbc
fix more errors
bshaffer Mar 7, 2023
0ba28f2
fix owlbot substitution for Tasks
bshaffer Mar 7, 2023
29e59db
fix owlbot substitution for scheduluer
bshaffer Mar 7, 2023
d266ec1
remove GrpcClient from Deploy
bshaffer Mar 7, 2023
7ff7814
Merge branch 'main' into add-phpstan
bshaffer Mar 8, 2023
2f1e414
remove reference to deprecated ReadOnly
bshaffer Mar 8, 2023
b73e736
remove phpstan
bshaffer Mar 8, 2023
e12c1e2
erge branch 'main' into add-phpstan
bshaffer Apr 7, 2023
77fa2cc
remove changes to PBReadOnly
bshaffer Apr 8, 2023
c1ab058
remove static => self
bshaffer Apr 8, 2023
ce5e94a
Merge branch 'main' into add-phpstan
bshaffer Apr 19, 2023
3ba1022
fix some phpstan errors
bshaffer Apr 19, 2023
14451a4
misc fixes for cs
bshaffer Apr 20, 2023
6051129
add composer install;
bshaffer Apr 20, 2023
8cda749
Merge branch 'main' into add-phpstan
vishwarajanand Apr 20, 2023
7386e2a
add temp bootstrapper
bshaffer Apr 25, 2023
cb4eb6e
revert removal of CloudDeployGrpcClient
bshaffer Apr 25, 2023
1fadf13
reflection handlers have been removed
bshaffer Apr 25, 2023
c83fdc6
Merge branch 'main' into add-phpstan
bshaffer May 1, 2023
685edbe
chore(deps): add support for common protos 4.0
bshaffer May 1, 2023
98c921e
Merge branch 'main' into add-phpstan
bshaffer May 1, 2023
7e8bf09
fix static analytis for biqquerystorage descriptor_fix
bshaffer May 1, 2023
6ed5377
Merge branch 'add-phpstan' of github.com:googleapis/google-cloud-php …
bshaffer May 1, 2023
adff980
add metadata dirs
bshaffer May 1, 2023
4e4f42f
Merge branch 'main' into add-phpstan
bshaffer May 2, 2023
d659027
add config for performance
bshaffer May 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
- name: "Install dependencies"
- name: Install Dependencies
run: composer global require "squizlabs/php_codesniffer:3.*"
- name: Run PHPCS Code Style Checker
run: ~/.composer/vendor/bin/phpcs --standard=phpcs-ruleset.xml -p
staticanalysis:
runs-on: ubuntu-latest
name: PHPStan Static Analysis
steps:
- uses: actions/checkout@v3
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- name: "Install dependencies"
run: |
composer --no-interaction --no-ansi --no-progress update
composer --no-interaction --no-ansi --no-progress update -d dev
- name: Run Static Analysis
run: |
dev/sh/static-analysis
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
run: |
vendor/bin/phpunit -c phpunit-snippets.xml.dist --verbose

test_package:
test_packages:
name: Package Tests
runs-on: ubuntu-latest
steps:
Expand Down
40 changes: 26 additions & 14 deletions BigQueryStorage/metadata/descriptor_fix.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,38 @@
* This file can be removed once a fix is implemented in the protobuf library
* @see https://github.com/protocolbuffers/protobuf/issues/11649
*/
namespace GPBMetadata\Google\Protobuf;
namespace GPBMetadata\Google\Protobuf {

class DescriptorFix
{
public static $is_initialized = false;
class DescriptorFix
{
public static $is_initialized = false;

public static function initOnce() {
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
public static function initOnce() {
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();

if (static::$is_initialized == true) {
return;
}
if (static::$is_initialized == true) {
return;
}

if ($pool instanceof \Google\Protobuf\Internal\DescriptorPool) {
// add a no-op reference for "google.protobuf.DescriptorProto"
$pool->addMessage('google.protobuf.DescriptorProto', \Google\Protobuf\Internal\Message::class)->finalizeToPool();
if ($pool instanceof \Google\Protobuf\Internal\DescriptorPool) {
// add a no-op reference for "google.protobuf.DescriptorProto"
$pool->addMessage('google.protobuf.DescriptorProto', \Google\Protobuf\DescriptorProto::class)->finalizeToPool();
}
static::$is_initialized = true;
}
static::$is_initialized = true;
}

DescriptorFix::initOnce();
}

DescriptorFix::initOnce();
namespace Google\Protobuf {

class DescriptorProto
{
public function __construct()
{
throw new \LogicException('Proto2 classes are not supported');
}
}
}

2 changes: 2 additions & 0 deletions Core/src/ClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ private function detectProjectId(array $config)
'and we were unable to detect a default project ID.'
);
}

return '';
}

/**
Expand Down
2 changes: 2 additions & 0 deletions Core/src/GrpcTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ private function formatValueForApi($value)

return ['list_value' => $this->formatListForApi($value)];
}

return [];
}

/**
Expand Down
1 change: 1 addition & 0 deletions Core/src/Lock/SymfonyLockAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

/**
* Symfony lock component adapter.
* @deprecated
*/
class SymfonyLockAdapter implements LockInterface
{
Expand Down
2 changes: 0 additions & 2 deletions Deploy/owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
'')




### [START] protoc backwards compatibility fixes

# roll back to private properties.
Expand Down
5 changes: 4 additions & 1 deletion Firestore/src/DocumentReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,10 @@ function ($collectionId) {
*/
protected function batchFactory()
{
return new WriteBatch(
if (!class_exists(WriteBatch::class)) {
class_alias(BulkWriter::class, WriteBatch::class);
}
return new BulkWriter(
$this->connection,
$this->valueMapper,
$this->databaseFromName($this->name)
Expand Down
5 changes: 4 additions & 1 deletion Firestore/src/FirestoreClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ public function __construct(array $config = [])
*/
public function batch()
{
return new WriteBatch(
if (!class_exists(WriteBatch::class)) {
class_alias(BulkWriter::class, WriteBatch::class);
}
return new BulkWriter(
$this->connection,
$this->valueMapper,
$this->databaseName(
Expand Down
3 changes: 2 additions & 1 deletion Scheduler/owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
# Fix missing formatting method
s.replace(
'src/V1/Gapic/CloudSchedulerGapicClient.php',
r"private static \$jobNameTemplate;\n\s{4}private static \$locationNameTemplate;",
r"private static \$jobNameTemplate;\n\n\s{4}private static \$locationNameTemplate;",
"""private static $jobNameTemplate;
private static $locationNameTemplate;

private static $projectNameTemplate;"""
)
s.replace(
Expand Down
3 changes: 2 additions & 1 deletion Scheduler/src/V1/Gapic/CloudSchedulerGapicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ class CloudSchedulerGapicClient
];

private static $jobNameTemplate;

private static $locationNameTemplate;

private static $projectNameTemplate;

private static $pathTemplateMap;

private static function getClientDefaults()
Expand Down
2 changes: 1 addition & 1 deletion Spanner/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ public function beginTransaction(array $args)
? PBReadOnly::class
: 'Google\Cloud\Spanner\V1\TransactionOptions\ReadOnly';
$readOnly = $this->serializer->decodeMessage(
new $readOnlyClass(),
new $readOnlyClass(), // @phpstan-ignore-line
$transactionOptions['readOnly']
);
$options->setReadOnly($readOnly);
Expand Down
2 changes: 1 addition & 1 deletion Spanner/src/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function name()
* @param int $expiration [optional] The Unix timestamp in seconds upon
* which the session will expire. **Defaults to** now plus 60
* minutes.
* @return int
* @return void
*/
public function setExpiration($expiration = null)
{
Expand Down
2 changes: 1 addition & 1 deletion Storage/src/ObjectPageIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function current()
/**
* Add new prefixes to the list.
*
* @return array
* @return void
*/
private function updatePrefixes()
{
Expand Down
3 changes: 2 additions & 1 deletion Tasks/owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@
]
s.replace(
tasksBrokenGapics,
r"private static \$locationNameTemplate;\n\s{4}private static \$queueNameTemplate;",
r"private static \$locationNameTemplate;\n\n\s{4}private static \$queueNameTemplate;",
"""private static $locationNameTemplate;
private static $projectNameTemplate;

private static $queueNameTemplate;"""
)
s.replace(
Expand Down
1 change: 1 addition & 0 deletions Tasks/src/V2/Gapic/CloudTasksGapicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class CloudTasksGapicClient
];

private static $locationNameTemplate;
private static $projectNameTemplate;

private static $queueNameTemplate;

Expand Down
1 change: 1 addition & 0 deletions Tasks/src/V2beta2/Gapic/CloudTasksGapicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class CloudTasksGapicClient
];

private static $locationNameTemplate;
private static $projectNameTemplate;

private static $queueNameTemplate;

Expand Down
1 change: 1 addition & 0 deletions Tasks/src/V2beta3/Gapic/CloudTasksGapicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class CloudTasksGapicClient
];

private static $locationNameTemplate;
private static $projectNameTemplate;

private static $queueNameTemplate;

Expand Down
3 changes: 2 additions & 1 deletion dev/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"symfony/process": "^6.0",
"google/cloud": "dev-parent",
"symfony/filesystem": "^6.0",
"twig/twig": "^3.5"
"twig/twig": "^3.5",
"phpstan/phpstan": "^1.7"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions dev/sh/static-analysis
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# Create a temporary bootstrap file that combines the autoloader of the project and the dev autoloader
TMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir')
echo "<?php require 'vendor/autoload.php';require 'dev/vendor/autoload.php';" > $TMPDIR/phpstan-bootstrap.php

# Run phpstan
dev/vendor/bin/phpstan analyse */src */metadata --autoload-file=$TMPDIR/phpstan-bootstrap.php
2 changes: 2 additions & 0 deletions dev/src/Command/AddComponentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('');
$output->writeln('');
$output->writeln('Success!');

return 0;
}

private function loadProtoContent(string $proto): string
Expand Down
16 changes: 9 additions & 7 deletions dev/src/Command/ComponentInfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Google\Cloud\Dev\Command;

use Google\Cloud\Dev\ComponentNamer;
use Google\Cloud\Dev\NewComponent;
use Google\Cloud\Dev\Component;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -79,6 +79,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
;
$table->render();
}

return 0;
}

private function checkComponent(Component $component, bool $verbose): array
Expand All @@ -97,24 +99,24 @@ private function checkComponent(Component $component, bool $verbose): array
}

if ($verbose && $protobufPackage) {
$namer = new ComponentNamer($protobufPackage);
$namer = new NewComponent();
// Output validation errors
$expectedComposerPackage = $namer->getComposerPackage();
$expectedGithubRepo = $namer->getGithubRepo();
$expectedNamespace = $namer->getPhpNamespace();
$expectedComposerPackage = $namer->composerPackage;
$expectedGithubRepo = $namer->githubRepo;
$expectedNamespace = $namer->phpNamespace;
if ($expectedGithubRepo !== $githubRepo) {
$validationErrors[] = "Github repo name $githubRepo doesn't match expected $expectedGithubRepo";
}
if ($expectedComposerPackage !== $composerPackage) {
$validationErrors[] = "Composer package name $composerPackage doesn't match expected $expectedComposerPackage";
}

$expectedComponentName = $namer->getComponentName();
$expectedComponentName = $namer->componentName;
if (strtolower($componentName) !== strtolower($expectedComponentName)) {
$validationErrors[] = "Component $componentName doesn't match expected $expectedComponentName";
}

$expectedNamespace = $namer->getPhpNamespace();
$expectedNamespace = $namer->phpNamespace;
if (strtolower($expectedNamespace) !== strtolower($phpNamespace)) {
$validationErrors[] = "PHP namespace $phpNamespace doesn't match expected $expectedNamespace";
}
Expand Down
2 changes: 2 additions & 0 deletions dev/src/Command/DocFxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$process->mustRun();
$output->writeln('Done.');
}

return 0;
}

public static function getPhpDocCommand(string $componentPath, string $outDir): Process
Expand Down
2 changes: 2 additions & 0 deletions dev/src/Command/ReleaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
'Release %s generated!',
$version
));

return 0;
}

private function getNextVersionName($type, array $component)
Expand Down
2 changes: 2 additions & 0 deletions dev/src/Command/SplitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class SplitCommand extends Command
const EXEC_DIR = '.split';
const TOKEN_ENV = 'GH_OAUTH_TOKEN';

private string $rootPath;

/**
* @param string $rootPath The path to the repository root directory.
*/
Expand Down
4 changes: 2 additions & 2 deletions dev/src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ private function validateComponentFiles(): void
if (empty($repoMetadataJson['release_level'])) {
throw new RuntimeException(sprintf(
'repo metadata does not contain "release_level" for component "%s"',
$component
$this->name
));
}
if (empty($repoMetadataJson['release_level'])) {
throw new RuntimeException(sprintf(
'repo metadata does not contain "client_documentation" for component "%s"',
$component
$this->name
));
}
$this->releaseLevel = $repoMetadataJson['release_level'];
Expand Down
26 changes: 26 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
parameters:
level: 0
excludePaths:
# Ignore classes not compatible with Monolog2
- Core/src/Logger/*.php
# Ignore deprecated classes
- Core/src/Lock/SymfonyLockAdapter.php
- Core/src/PhpArray.php
# Ignore classes using the deprecated stackriver_debugger extension
- Debugger/src/Agent.php
- Debugger/src/Breakpoint.php
# Ignore Monolog3 for now, as these tests run using Monolog2
- Logging/src/LogMessageProcessor/MonologV3MessageProcessor.php
- Logging/src/LogMessageProcessor/MonologV3MessageProcessor.php
# Ignore CloudDeployGrpcClient.php because it has a method name conflict
- Deploy/src/V1/CloudDeployGrpcClient.php
ignoreErrors:
# Problems with legacy namespaces (I'm not sure why these are throwing errors)
- '#^Class Google\\[A-Za-z1-9\\]+_[A-Za-z1-9_]+ not found\.$#'
# Protobuf constant classes sometimes contain multiple values for one array key
- '#Array has \d duplicate keys with value #'
# Ignore this error because I don't consider it to be level 0
- '#Unsafe usage of new static#'
includes:
- dev/vendor/phpstan/phpstan/conf/bleedingEdge.neon