Braintree
has 29 functions (exceeds 20 allowed). Consider refactoring. Open
class Braintree extends Component
{
public $environment = 'sandbox';
public $merchantId;
public $publicKey;
The class Braintree has 21 public methods. Consider refactoring Braintree to keep number of public methods under 10. Open
class Braintree extends Component
{
public $environment = 'sandbox';
public $merchantId;
public $publicKey;
- Read upRead up
- Exclude checks
TooManyPublicMethods
Since: 0.1
A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.
Example
Source https://phpmd.org/rules/codesize.html#toomanypublicmethods
Function setOptions
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public function setOptions(array $values): Braintree
{
if (!empty($values)) {
foreach ($values as $key => $value) {
if ($key === 'amount') {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Class "Braintree" has 29 methods, which is greater than 20 authorized. Split it into smaller classes. Open
class Braintree extends Component
- Read upRead up
- Exclude checks
A class that grows too much tends to aggregate too many responsibilities and inevitably becomes harder to understand and therefore to maintain. Above a specific threshold, it is strongly advised to refactor the class into smaller ones which focus on well defined topics.
The class Braintree has a coupling between objects value of 17. Consider to reduce the number of dependencies under 13. Open
class Braintree extends Component
{
public $environment = 'sandbox';
public $merchantId;
public $publicKey;
- Read upRead up
- Exclude checks
CouplingBetweenObjects
Since: 1.1.0
A class with too many dependencies has negative impacts on several quality aspects of a class. This includes quality criteria like stability, maintainability and understandability
Example
class Foo {
/**
* @var \foo\bar\X
*/
private $x = null;
/**
* @var \foo\bar\Y
*/
private $y = null;
/**
* @var \foo\bar\Z
*/
private $z = null;
public function setFoo(\Foo $foo) {}
public function setBar(\Bar $bar) {}
public function setBaz(\Baz $baz) {}
/**
* @return \SplObjectStorage
* @throws \OutOfRangeException
* @throws \InvalidArgumentException
* @throws \ErrorException
*/
public function process(\Iterator $it) {}
// ...
}
Source https://phpmd.org/rules/design.html#couplingbetweenobjects
The method getPlanById has a boolean flag argument $allowCaching, which is a certain sign of a Single Responsibility Principle violation. Open
public function getPlanById(string $planId, bool $allowCaching = true): ?Plan
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
The method sale has a boolean flag argument $storeInVaultOnSuccess, which is a certain sign of a Single Responsibility Principle violation. Open
public function sale(bool $submitForSettlement = true, bool $storeInVaultOnSuccess = true): array
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
The method getPlanIds has a boolean flag argument $allowCaching, which is a certain sign of a Single Responsibility Principle violation. Open
public function getPlanIds(bool $allowCaching = true): array
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
The method sale has a boolean flag argument $submitForSettlement, which is a certain sign of a Single Responsibility Principle violation. Open
public function sale(bool $submitForSettlement = true, bool $storeInVaultOnSuccess = true): array
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
The method getAllPlans has a boolean flag argument $allowCaching, which is a certain sign of a Single Responsibility Principle violation. Open
public function getAllPlans(bool $allowCaching = true): array
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
Define a constant instead of duplicating this literal "result" 9 times. Open
return ['status' => $result->success, 'result' => $result];
- 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.
Rename "$plans" which has the same name as the field declared at line 51. Wontfix
$plans = $this->getAllPlans($allowCaching);
- Read upRead up
- Exclude checks
Shadowing fields with a local variable is a bad practice that reduces code readability: it makes it confusing to know whether the field or the variable is being used.
Noncompliant Code Example
class Foo { public $myField; public function doSomething() { $myField = 0; ... } }
See
- CERT, DCL51-J. - Do not shadow or obscure identifiers in subscopes
Define a constant instead of duplicating this literal "status" 9 times. Open
return ['status' => $result->success, 'result' => $result];
- 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.
Define a constant instead of duplicating this literal "options" 3 times. Open
$this->options['options']['submitForSettlement'] = $submitForSettlement;
- 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.
Define a constant instead of duplicating this literal "amount" 5 times. Open
'amount' => $amount,
- 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.
Define a constant instead of duplicating this literal "customerId" 8 times. Open
if (isset($this->options['customerId'])) {
- 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.
Rename "$plans" which has the same name as the field declared at line 51. Wontfix
$plans = $this->getAllPlans($allowCaching);
- Read upRead up
- Exclude checks
Shadowing fields with a local variable is a bad practice that reduces code readability: it makes it confusing to know whether the field or the variable is being used.
Noncompliant Code Example
class Foo { public $myField; public function doSomething() { $myField = 0; ... } }
See
- CERT, DCL51-J. - Do not shadow or obscure identifiers in subscopes
Define a constant instead of duplicating this literal "billing" 3 times. Open
if (isset($this->options['billing'])) {
- 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.
Define a constant instead of duplicating this literal "creditCard" 4 times. Open
$sendArray = $this->options['creditCard'];
- 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.
Call to method merchantId
from undeclared class \Braintree\Configuration
Open
Configuration::$attribute($this->$attribute);
- Exclude checks
Assigning array<string></string>
to property but \tuyakhov\braintree\Braintree->options
is array<string>>|array<string></string></string>
Open
$this->options['amount'] = round($amount, 2);
- Exclude checks
Class extends undeclared class \yii\base\Component
Open
class Braintree extends Component
- Exclude checks
Possibly zero references to public property \tuyakhov\braintree\Braintree->environment
Open
public $environment = 'sandbox';
- Exclude checks
Assigning array<string></string>
to property but \tuyakhov\braintree\Braintree->options
is array<string></string>
Open
$this->options['options']['storeInVaultOnSuccess'] = $storeInVaultOnSuccess;
- Exclude checks
Call to method sale
from undeclared class \Braintree\Transaction
Open
$result = Transaction::sale($this->options);
- Exclude checks
Return type of getAllPlans()
is undeclared type \Braintree\Plan[]
Open
public function getAllPlans(bool $allowCaching = true): array
- Exclude checks
Return type of getPlanById()
is undeclared type ?\Braintree\Plan
Open
public function getPlanById(string $planId, bool $allowCaching = true): ?Plan
- Exclude checks
Call to method __construct
from undeclared class \yii\base\InvalidConfigException
Open
throw new InvalidConfigException(
- Exclude checks
Call to method delete
from undeclared class \Braintree\PaymentMethod
Open
$result = PaymentMethod::delete($this->options['paymentMethodToken']);
- Exclude checks
Reference to undeclared class \yii\base\Component
Open
parent::init();
- Exclude checks
Call to method create
from undeclared class \Braintree\Address
Open
$result = Address::create($sendArray);
- Exclude checks
@throws type of init
has undeclared type \yii\base\InvalidConfigException
Open
public function init()
- Exclude checks
Call to method generate
from undeclared class \Braintree\ClientToken
Open
$this->clientToken = ClientToken::generate($params);
- Exclude checks
Call to method sale
from undeclared class \Braintree\Transaction
Open
$result = Transaction::sale(
- Exclude checks
Call to method all
from undeclared class \Braintree\Plan
Open
$this->plans = Plan::all();
- Exclude checks
Call to method create
from undeclared class \Braintree\CreditCard
Open
$result = CreditCard::create($sendArray);
- Exclude checks
Reference to instance property id
from undeclared class \Braintree\Plan
Open
if ($plan->id === $planId) {
- Exclude checks
Return type of findTransaction()
is undeclared type \Braintree\Transaction
Open
public function findTransaction(string $id): Transaction
- Exclude checks
Call to method sale
from undeclared class \Braintree\Transaction
Open
$result = Transaction::sale(
- Exclude checks
Return type of searchTransaction()
is undeclared type \Braintree\ResourceCollection
Open
public function searchTransaction($params = []): ResourceCollection
- Exclude checks
Possibly zero write references to public property \tuyakhov\braintree\Braintree->merchantId
Open
public $merchantId;
- Exclude checks
Property \tuyakhov\braintree\Braintree->plans
has undeclared type \Braintree\Plan[]
Open
protected $plans;
- Exclude checks
Possibly zero references to public method \tuyakhov\braintree\Braintree::getUrlManager()
Open
public function getUrlManager(): UrlManager
- Exclude checks
Call to method update
from undeclared class \Braintree\PaymentMethod
Open
$result = PaymentMethod::update($this->options['paymentMethodToken'], $this->options['paymentMethod']);
- Exclude checks
Assigning array<string></string>
to property but \tuyakhov\braintree\Braintree->options
is array<string></string>
Open
$this->options['customer']['id'] = $this->options['customerId'];
- Exclude checks
Possibly zero references to public property \tuyakhov\braintree\Braintree->privateKey
Open
public $privateKey;
- Exclude checks
Possibly zero references to public method \tuyakhov\braintree\Braintree::init()
Open
public function init()
- Exclude checks
Argument #2 of this call to \strtr
is typically a literal or constant but isn't, but argument #1 (which is typically a variable) is a literal or constant. The arguments may be in the wrong order. Invalid
strtr(
- Exclude checks
Reference to instance property id
from undeclared class \Braintree\Plan
Open
$planIds[] = $plan->id;
- Exclude checks
Possibly zero references to public property \tuyakhov\braintree\Braintree->publicKey
Open
public $publicKey;
- Exclude checks
Call to method create
from undeclared class \Braintree\PaymentMethod
Open
$result = PaymentMethod::create($this->options['paymentMethod']);
- Exclude checks
Call to method create
from undeclared class \Braintree\Customer
Open
$result = Customer::create($this->options['customer']);
- Exclude checks
Call to method getValue
from undeclared class \yii\helpers\ArrayHelper
Open
$optionalValue = ArrayHelper::getValue($values, $optionalParamName);
- Exclude checks
Possibly zero references to public method \tuyakhov\braintree\Braintree::getClientToken()
Open
public function getClientToken($params = []): string
- Exclude checks
Return type of getPlanById()
is undeclared type \Braintree\Plan
Open
public function getPlanById(string $planId, bool $allowCaching = true): ?Plan
- Exclude checks
Possibly zero references to public method \tuyakhov\braintree\Braintree::findTransaction()
Open
public function findTransaction(string $id): Transaction
- Exclude checks
Call to method find
from undeclared class \Braintree\Transaction
Open
return Transaction::find($id);
- Exclude checks
Return type of findMerchant()
is undeclared type \Braintree\MerchantAccount
Open
public function findMerchant(string $merchantId): MerchantAccount
- Exclude checks
Return type of createSubscription()
is undeclared type \Braintree\Result\Error
(Did you mean class \Error) Open
public function createSubscription(array $params)
- Exclude checks
Return type of updateSubscription()
is undeclared type \Braintree\Result\Successful
Open
public function updateSubscription(string $subscriptionId, array $params)
- Exclude checks
Return type of parseWebhookNotification()
is undeclared type \Braintree\WebhookNotification
Open
public function parseWebhookNotification(string $signature, $payload): WebhookNotification
- Exclude checks
Call to method submitForSettlement
from undeclared class \Braintree\Transaction
Open
$result = Transaction::submitForSettlement($retryResult->transaction->id);
- Exclude checks
Call to method search
from undeclared class \Braintree\Transaction
Open
return Transaction::search($params);
- Exclude checks
Call to method cancel
from undeclared class \Braintree\Subscription
Open
return Subscription::cancel($subscriptionId);
- Exclude checks
Return type of searchSubscription()
is undeclared type \Braintree\ResourceCollection
Open
public function searchSubscription($params = []): ResourceCollection
- Exclude checks
Return type of findSubscription()
is undeclared type \Braintree\Subscription
Open
public function findSubscription(string $subscriptionId): Subscription
- Exclude checks
Call to method find
from undeclared class \Braintree\MerchantAccount
Open
return MerchantAccount::find($merchantId);
- Exclude checks
Call to method find
from undeclared class \Braintree\Customer
Open
return Customer::find($customerId);
- Exclude checks
Return type of createSubscription()
is undeclared type \Braintree\Result\Successful
Open
public function createSubscription(array $params)
- Exclude checks
Return type of cancelSubscription()
is undeclared type \Braintree\Result\Successful
Open
public function cancelSubscription(string $subscriptionId)
- Exclude checks
Call to method parse
from undeclared class \Braintree\WebhookNotification
Open
return WebhookNotification::parse($signature, $payload);
- Exclude checks
Call to method find
from undeclared class \Braintree\Subscription
Open
return Subscription::find($subscriptionId);
- Exclude checks
Call to method update
from undeclared class \Braintree\Subscription
Open
return Subscription::update($subscriptionId, $params);
- Exclude checks
Return type of cancelSubscription()
is undeclared type \Braintree\Result\Error
(Did you mean class \Error) Open
public function cancelSubscription(string $subscriptionId)
- Exclude checks
Call to method retryCharge
from undeclared class \Braintree\Subscription
Open
$retryResult = Subscription::retryCharge($subscriptionId, $amount);
- Exclude checks
Call to method search
from undeclared class \Braintree\Subscription
Open
return Subscription::search($params);
- Exclude checks
Call to method create
from undeclared class \Braintree\Subscription
Open
return Subscription::create($params);
- Exclude checks
Return type of findCustomer()
is undeclared type \Braintree\Customer
Open
public function findCustomer(string $customerId): Customer
- Exclude checks
Return type of updateSubscription()
is undeclared type \Braintree\Result\Error
(Did you mean class \Error) Open
public function updateSubscription(string $subscriptionId, array $params)
- Exclude checks
Avoid excessively long variable names like $storeInVaultOnSuccess. Keep variable name length under 20. Open
public function sale(bool $submitForSettlement = true, bool $storeInVaultOnSuccess = true): array
- Read upRead up
- Exclude checks
LongVariable
Since: 0.2
Detects when a field, formal or local variable is declared with a long name.
Example
class Something {
protected $reallyLongIntName = -3; // VIOLATION - Field
public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
$otherReallyLongName = -5; // VIOLATION - Local
for ($interestingIntIndex = 0; // VIOLATION - For
$interestingIntIndex < 10;
$interestingIntIndex++ ) {
}
}
}
Source https://phpmd.org/rules/naming.html#longvariable
Avoid variables with short names like $id. Configured minimum length is 3. Wontfix
public function findTransaction(string $id): Transaction
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}