Skip to content
This repository was archived by the owner on Sep 14, 2023. It is now read-only.

Commit 7fd101d

Browse files
committed
more phpstan fixes
1 parent 6b52445 commit 7fd101d

11 files changed

+55
-27
lines changed

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ parameters:
77
- '#Instantiated class directapi\\components\\Enum is abstract#'
88
- '#.*should be covariant with return type.*#'
99
- '#.*should be contravariant with parameter.*#'
10-
- '#.*expects array<*#'
1110
- '#.*does not accept array<*#'
1211
checkMissingIterableValueType: false
1312
treatPhpDocTypesAsCertain: false

src/adwords/AdWordsAdGroupCriterionsProvider.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace sitkoru\contextcache\adwords;
44

55
use function count;
6+
use ErrorException;
67
use Exception;
78
use Google\AdsApi\AdWords\AdWordsSession;
89
use Google\AdsApi\AdWords\v201809\cm\AdGroupCriterion;
@@ -198,7 +199,9 @@ public function getByAdGroupIds(array $adGroupIds, array $predicates = []): arra
198199
if (count($notFound) > 0) {
199200
$selector = new Selector();
200201
$selector->setFields(self::$fields);
201-
$predicates[] = new Predicate('AdGroupId', PredicateOperator::IN, $notFound);
202+
$predicates[] = new Predicate('AdGroupId', PredicateOperator::IN, array_map(function (int $id): string {
203+
return $id . '';
204+
}, $notFound));
202205
$selector->setPredicates($predicates);
203206
$fromService = $this->doRequest(function () use ($selector): array {
204207
/**
@@ -242,7 +245,9 @@ public function getByCampaignIds(array $campaignIds, array $predicates = []): ar
242245
if (count($notFound) > 0) {
243246
$selector = new Selector();
244247
$selector->setFields(self::$fields);
245-
$predicates[] = new Predicate('CampaignId', PredicateOperator::IN, $notFound);
248+
$predicates[] = new Predicate('CampaignId', PredicateOperator::IN, array_map(function (int $id): string {
249+
return $id . '';
250+
}, $notFound));
246251
$selector->setPredicates($predicates);
247252
$perPageSize = 10000;
248253
$selector->setPaging(new Paging(0, $perPageSize));
@@ -294,7 +299,11 @@ public function update(array $entities): UpdateResult
294299
*/
295300
$this->logger->info('Update chunk #' . $i . '. Size: ' . count($updateChunk));
296301
$jobResults = $this->runMutateJob($updateChunk);
297-
$this->processJobResult($result, $jobResults);
302+
if (is_array($jobResults)) {
303+
$this->processJobResult($result, $jobResults);
304+
} else {
305+
throw new ErrorException('Empty result from google');
306+
}
298307
}
299308
} else {
300309
$mutateResult = $this->adGroupCriterionService->mutate($updateOperations);

src/adwords/AdWordsAdGroupsProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace sitkoru\contextcache\adwords;
44

55
use function count;
6+
use ErrorException;
67
use Exception;
78
use Google\AdsApi\AdWords\AdWordsSession;
89
use Google\AdsApi\AdWords\v201809\cm\AdGroup;
@@ -146,7 +147,9 @@ public function getByCampaignIds(array $campaignIds, array $predicates = []): ar
146147
if (count($notFound) > 0) {
147148
$selector = new Selector();
148149
$selector->setFields(self::$fields);
149-
$predicates[] = new Predicate('CampaignId', PredicateOperator::IN, $notFound);
150+
$predicates[] = new Predicate('CampaignId', PredicateOperator::IN, array_map(function (int $id): string {
151+
return $id . '';
152+
}, $notFound));
150153
$selector->setPredicates($predicates);
151154
$fromService = $this->doRequest(function () use ($selector): array {
152155
/**
@@ -195,7 +198,11 @@ public function update(array $entities): UpdateResult
195198
foreach (array_chunk($addOperations, self::MAX_OPERATIONS_SIZE) as $i => $addChunk) {
196199
$this->logger->info('Update chunk #' . $i . '. Size: ' . count($addChunk));
197200
$jobResults = $this->runMutateJob($addChunk);
198-
$this->processJobResult($result, $jobResults);
201+
if (is_array($jobResults)) {
202+
$this->processJobResult($result, $jobResults);
203+
} else {
204+
throw new ErrorException('Empty result from google');
205+
}
199206
}
200207
} else {
201208
$mutateResult = $this->adGroupService->mutate($addOperations);

src/adwords/AdWordsAdsProvider.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ public function getByCampaignIds(array $campaignIds, array $predicates = []): ar
271271
if (count($notFound) > 0) {
272272
$selector = new Selector();
273273
$selector->setFields(self::$fields);
274-
$predicates[] = new Predicate('CampaignId', PredicateOperator::IN, $notFound);
274+
$predicates[] = new Predicate('CampaignId', PredicateOperator::IN, array_map(function (int $id): string {
275+
return $id . '';
276+
}, $notFound));
275277
$selector->setPredicates($predicates);
276278
$fromService = $this->doRequest(function () use ($selector): array {
277279
/**
@@ -314,7 +316,9 @@ public function getByAdGroupIds(array $adGroupIds, array $predicates = []): arra
314316
if (count($notFound) > 0) {
315317
$selector = new Selector();
316318
$selector->setFields(self::$fields);
317-
$predicates[] = new Predicate('AdGroupId', PredicateOperator::IN, $notFound);
319+
$predicates[] = new Predicate('AdGroupId', PredicateOperator::IN, array_map(function (int $id): string {
320+
return $id . '';
321+
}, $notFound));
318322
$selector->setPredicates($predicates);
319323
$fromService = $this->doRequest(function () use ($selector): array {
320324
/**
@@ -383,13 +387,17 @@ public function update(array $entities): UpdateResult
383387
*/
384388
$this->logger->info('Add chunk #' . $i . '. Size: ' . count($addChunk));
385389
$jobResults = $this->runMutateJob($addChunk);
386-
$this->processJobResult($result, $jobResults);
387-
if (!$result->success) {
388-
foreach ($result->errors as $adOperationId => $errors) {
389-
$adOperation = $addChunk[$adOperationId];
390-
$adId = $adOperation->getOperand()->getAd()->getId();
391-
unset($deleteOperations[$adId]);
390+
if (is_array($jobResults)) {
391+
$this->processJobResult($result, $jobResults);
392+
if (!$result->success) {
393+
foreach ($result->errors as $adOperationId => $errors) {
394+
$adOperation = $addChunk[$adOperationId];
395+
$adId = $adOperation->getOperand()->getAd()->getId();
396+
unset($deleteOperations[$adId]);
397+
}
392398
}
399+
} else {
400+
throw new ErrorException('Empty result from google');
393401
}
394402
}
395403

@@ -410,7 +418,11 @@ public function update(array $entities): UpdateResult
410418
}
411419
}
412420
}
413-
$this->processJobResult($result, $jobResults);
421+
if (is_array($jobResults)) {
422+
$this->processJobResult($result, $jobResults);
423+
} else {
424+
throw new ErrorException('Empty result from google');
425+
}
414426
}
415427
} else {
416428
$mutateResult = $this->adGroupAdService->mutate($addOperations);

src/adwords/AdWordsCampaignsProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ public function update(array $entities): UpdateResult
196196
foreach (array_chunk($addOperations, self::MAX_OPERATIONS_SIZE) as $i => $addChunk) {
197197
$this->logger->info('Update chunk #' . $i . '. Size: ' . count($addChunk));
198198
$jobResults = $this->runMutateJob($addChunk);
199-
$this->processJobResult($result, $jobResults);
199+
if (is_array($jobResults)) {
200+
$this->processJobResult($result, $jobResults);
201+
} else {
202+
throw new ErrorException('Empty result from google');
203+
}
200204
}
201205
} else {
202206
$mutateResult = $this->campaignService->mutate($addOperations);

src/adwords/AdWordsEntitiesProvider.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function runMutateJob(array $operations)
129129
'ProcessingErrors',
130130
'ProgressStats'
131131
]);
132-
$selector->setPredicates([new Predicate('Id', PredicateOperator::EQUALS, [$job->getId()])]);
132+
$selector->setPredicates([new Predicate('Id', PredicateOperator::EQUALS, [$job->getId() . ''])]);
133133
$batchJob = $this->batchJobService->get($selector)->getEntries()[0];
134134
$this->logger->info('Batch job ID ' . $batchJob->getId() . " has status '{$batchJob->getStatus()}'. Last sleep {$sleepSeconds}");
135135
$this->logger->debug('Batch job ID ' . $batchJob->getId() . " has status '{$batchJob->getStatus()}'");
@@ -186,18 +186,13 @@ public function runMutateJob(array $operations)
186186
* @param MutateResult[] $jobResult
187187
*
188188
* @return array
189-
*
190-
* @throws ErrorException
191189
*/
192-
protected function processErrors($jobResult): array
190+
protected function processErrors(array $jobResult): array
193191
{
194192
$skipped = [];
195193
$failed = [];
196194
$errors = [];
197195
$genericErrors = [];
198-
if (!is_array($jobResult)) {
199-
throw new ErrorException('Empty result from google');
200-
}
201196

202197
foreach ($jobResult as $mutateResult) {
203198
/**
@@ -310,10 +305,8 @@ protected function processMutateErrors(?array $mutateErrors = []): array
310305
* @param MutateResult[] $mutateResults
311306
*
312307
* @return UpdateResult
313-
*
314-
* @throws ErrorException
315308
*/
316-
protected function processJobResult(UpdateResult $jobResult, $mutateResults): UpdateResult
309+
protected function processJobResult(UpdateResult $jobResult, array $mutateResults): UpdateResult
317310
{
318311
/**
319312
* @var int[] $failed

src/common/cache/ContextNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function supportsNormalization($data, $format = null): bool
2828
*/
2929
public function supportsDenormalization($data, $type, $format = null): bool
3030
{
31-
return array_key_exists('_class', $data);
31+
return is_array($data) && array_key_exists('_class', $data);
3232
}
3333

3434
/**

src/direct/DirectAdGroupsProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public function update(array $entities): UpdateResult
182182
*/
183183
protected function getChanges(array $ids, string $date): ?CheckResponse
184184
{
185+
// @phpstan-ignore-next-line
185186
return $this->directApiService->getChangesService()->check(
186187
[],
187188
$ids,

src/direct/DirectAdsProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ public function update(array $entities): UpdateResult
273273
*/
274274
protected function getChanges(array $ids, string $date): ?CheckResponse
275275
{
276+
// @phpstan-ignore-next-line
276277
return $this->directApiService->getChangesService()->check([], [], $ids, [FieldNamesEnum::AD_IDS], $date);
277278
}
278279

src/direct/DirectCampaignsProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public function update(array $entities): UpdateResult
188188
*/
189189
protected function getChanges(array $ids, string $date): ?CheckResponse
190190
{
191+
// @phpstan-ignore-next-line
191192
return $this->directApiService->getChangesService()->check([], [], $ids, [FieldNamesEnum::CAMPAIGN_IDS], $date);
192193
}
193194

0 commit comments

Comments
 (0)