googleapis / google-api-php-client-services

http://googleapis.github.io/google-api-php-client-services/
Apache License 2.0
1.23k stars 314 forks source link

Return $this in setters to allow chaining #2319

Open Tofandel opened 1 year ago

Tofandel commented 1 year ago

Pretty much all the setters in the AnalyticsData lib return void, it would be much nicer to return $this in all setters to be able to chain them

Example: https://github.com/googleapis/google-api-php-client-services/blob/main/src/AnalyticsData/RunReportRequest.php

I wanted to write my requests like this

(new AnalyticsData\BatchRunReportsRequest())->setRequests([
    (new AnalyticsData\RunReportRequest())
    ->setDimensions([
        new AnalyticsData\Dimension(['name' => 'year']),
        new AnalyticsData\Dimension(['name' => 'month']),
        new AnalyticsData\Dimension(['name' => 'medium']),
    ])
    ->setDimensionFilter(...)
])

But without chaining, it becomes

$request1 = new AnalyticsData\RunReportRequest();
$request1->setDimensions([
    new AnalyticsData\Dimension(['name' => 'year']),
    new AnalyticsData\Dimension(['name' => 'month']),
    new AnalyticsData\Dimension(['name' => 'medium']),
]);
$request1->setDimensionFilter(...);
$batch = new AnalyticsData\BatchRunReportsRequest();
$batch->setRequests([$request1]);
bshaffer commented 1 year ago

Please use this analytics data client, as the one you are using is no longer supported.

composer require google/analytics-data