| Conditions | 6 |
| Paths | 3 |
| Total Lines | 56 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 31 | public function performQuery($siteUrl, $rows, $request): Collection |
||
| 32 | { |
||
| 33 | $searchanalyticsResource = $this->getWebmastersService()->searchanalytics; |
||
| 34 | |||
| 35 | $maxQueries = 2000; |
||
| 36 | $currentRequest = 1; |
||
| 37 | $dataRows = new Collection(); |
||
| 38 | |||
| 39 | while ($currentRequest < $maxQueries) { |
||
| 40 | $startRow = ($currentRequest-1) * self::CHUNK_SIZE; |
||
| 41 | |||
| 42 | $request->setRowLimit(self::CHUNK_SIZE); |
||
| 43 | $request->setStartRow($startRow); |
||
| 44 | |||
| 45 | $backoff = new ExponentialBackoff(10); |
||
| 46 | $response = $backoff->execute(function () use ($searchanalyticsResource, $siteUrl, $request) { |
||
| 47 | return $searchanalyticsResource->query($siteUrl, $request, $this->queryOptParams); |
||
| 48 | }); |
||
| 49 | |||
| 50 | // Stop if no more rows returned |
||
| 51 | if (count($response->getRows()) == 0) { |
||
| 52 | break; |
||
| 53 | } |
||
| 54 | |||
| 55 | foreach ($response->getRows() as $row) { |
||
| 56 | /* |
||
| 57 | * Use a unique hash as key to prevent duplicates caused by the query dimension problem with the google api |
||
| 58 | * Google give less than 5000 rows back when two or more dimension with the query dimension are choosen, repeated calls give back more rows |
||
| 59 | * https://productforums.google.com/forum/?hl=en#!topic/webmasters/wF_Rm9CGr4U |
||
| 60 | */ |
||
| 61 | |||
| 62 | $uniqueHash = md5(str_random()); |
||
| 63 | if (count($row->getKeys())) { |
||
| 64 | $item = array_combine($request->getDimensions(), $row->getKeys()); |
||
| 65 | $uniqueHash = md5(implode('', $row->getKeys()) . $request->getSearchType()); |
||
| 66 | } |
||
| 67 | |||
| 68 | $item['clicks'] = $row->getClicks(); |
||
|
|
|||
| 69 | $item['impressions'] = $row->getImpressions(); |
||
| 70 | $item['ctr'] = $row->getCtr(); |
||
| 71 | $item['position'] = $row->getPosition(); |
||
| 72 | $item['searchType'] = $request->getSearchType(); |
||
| 73 | |||
| 74 | $dataRows->put($uniqueHash, $item); |
||
| 75 | } |
||
| 76 | |||
| 77 | //Stop if the requested row count are reached |
||
| 78 | if ($dataRows->count() >= $rows) { |
||
| 79 | break; |
||
| 80 | } |
||
| 81 | |||
| 82 | $currentRequest++; |
||
| 83 | } |
||
| 84 | |||
| 85 | return $dataRows->take($rows); |
||
| 86 | } |
||
| 87 | |||
| 119 | } |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: