Avoid using undefined variables such as '$resolved' which will lead to PHP notices. Open
$property = array_merge($property, $resolved);
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Avoid using undefined variables such as '$resolved' which will lead to PHP notices. Open
$resolved['oneOf'] = $resolvedTypes;
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Missing class import via use statement (line '150', column '19'). Open
$nugget = new \Activerules\Nugget\Nugget();
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
The method hydrateTypes uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
hydrateOneOfType($property, $types);
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
The method resolveType uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
// Assume we got a valid Schema name
$resolved['$ref'] = "#/components/schema/" . $type;
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
Define a constant instead of duplicating this literal "types" 3 times. Open
$property['types'] = explode(' or ', $propertyRow[1]);
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Suspicious attempt to unset an offset of a value of type string
Open
unset($property['types']);
- Exclude checks
Variable $resolved
was undeclared, but array fields are being added to it. Open
$resolved['oneOf'] = $resolvedTypes;
- Exclude checks
Returning type array{$ref:string}|array{type:,nuggetType:string}
but resolveType()
is declared to return string
Open
return $resolved;
- Exclude checks
Suspicious array access to \type
Open
return file_exists(PROPERTYDIR + DIRECTORY_SEPARATOR + $property['name'] + '.json');
- Exclude checks
Call to method __construct
from undeclared class \Google_Client
Open
$client = new Google_Client();
- Exclude checks
Reference to instance property spreadsheets_values
from undeclared class \Google_Service_Sheets
Open
$response = $service->spreadsheets_values->get($spreadsheetID, $range);
- Exclude checks
Parameter $properties
has undeclared type \type
Open
function processProperties($properties)
- Exclude checks
Return type of propertyExists()
is undeclared type \type
Open
function propertyExists($property)
- Exclude checks
Argument 1 (filename)
is float|int
but \file_exists()
takes string
Open
return file_exists(PROPERTYDIR + DIRECTORY_SEPARATOR + $property['name'] + '.json');
- Exclude checks
Argument 1 (property)
is string
but \propertyExists()
takes \type
defined at /code/src/bin/readPropertiesFromGoogle.php:192
Open
if (propertyExists($property)) {
- Exclude checks
When fetching an array index from a value of type string,
found an array index of type 'types',
but expected the index to be of type int
Open
$types = $property['types'];
- Exclude checks
Argument 1 (arr1)
is string
but \array_merge()
takes array
Open
$property = array_merge($property, $resolved);
- Exclude checks
Parameter $property
has undeclared type \type
Open
function propertyExists($property)
- Exclude checks
Reference to constant SPREADSHEETS_READONLY
from undeclared class \Google_Service_Sheets
Open
define('SCOPES', implode(' ', array(Google_Service_Sheets::SPREADSHEETS_READONLY)));
- Exclude checks
Argument 1 (property)
is array{description:mixed,types:array<int>,name:mixed}</int>
but \processProperty()
takes string
defined at /code/src/bin/readPropertiesFromGoogle.php:71
Open
processProperty($property);
- Exclude checks
When fetching an array index from a value of type string,
found an array index of type 'name',
but expected the index to be of type int
Open
$propertyName = $property['name'];
- Exclude checks
Returning type bool
but propertyExists()
is declared to return \type
Open
return file_exists(PROPERTYDIR + DIRECTORY_SEPARATOR + $property['name'] + '.json');
- Exclude checks
Call to method setScopes
from undeclared class \Google_Client
Open
$client->setScopes(SCOPES);
- Exclude checks
Call to method __construct
from undeclared class \Google_Service_Sheets
Open
$service = new Google_Service_Sheets($client);
- Exclude checks
Method \processProperty
is declared to return string
but has no return value Open
function processProperty($property)
- Exclude checks
Call to undeclared method \Activerules\Nugget\Nugget::jsonType
Open
$jsonType = $nugget->jsonType($type);
- Exclude checks
Argument 1 (arr1)
is string
but \array_merge()
takes array
Open
$property = array_merge($property, $typeData);
- Exclude checks
Argument 1 (var)
is string
but \count()
takes \Countable|array
Open
if (count($types) === 1) {
- Exclude checks
Call to method useApplicationDefaultCredentials
from undeclared class \Google_Client
Open
$client->useApplicationDefaultCredentials();
- Exclude checks
Suspicious attempt to unset an offset of a value of type string
Open
unset($property['name']);
- Exclude checks
Returning type void
but processProperty()
is declared to return string
Open
return;
- Exclude checks
Argument 2 (types)
is string
but \hydrateOneOfType()
takes array
defined at /code/src/bin/readPropertiesFromGoogle.php:117
Open
hydrateOneOfType($property, $types);
- Exclude checks
Argument 2 (args)
is string
but \array_merge()
takes array
Open
$property = array_merge($property, $typeData);
- Exclude checks
A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 19 and the first side effect is on line 3. Open
<?php
- Exclude checks