PHP code example of tasko-products / php-codestyle
1. Go to this page and download the library: Download tasko-products/php-codestyle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
/** Get the user details */
public function getUserSalary(int $id): string
{
/** Fetch the user details by id */
$user = User::where('id', $id)->first();
/** Calling function to calculate user's current month salary */
$this->currentMonthSalary($user);
//...
}
/** Calculating the current month salary of user */
private function currentMonthSalary(User $user): void
{
/** Salary = (Total Days - Leave Days) * PerDay Salary */
// ...
}
public function getUserSalary(int $id): void
{
$user = User::where('id', $id)->first();
$this->currentMonthSalary($user);
//...
}
private function currentMonthSalary(User $user): string
{
/** Salary = (Total Days - Leave Days) * PerDay Salary */
// ...
}
public function getUserDetailsByEmail($email): array
{
// ...
}
// What the heck is 1 & 2 for?
if ($user->gender == 1) {
// ...
}
if ($user->gender == 2) {
// ...
}
public const MALE = 1;
public const FEMALE = 2;
if ($user->gender === MALE) {
// ...
}
if ($user->gender === FEMALE) {
// ...
}
public function calculatePrice($quantity, $pricePerUnit): float {
$price = 0;
if ($quantity > 0) {
$price = $quantity * $pricePerUnit;
if ($price > 100) {
//...
}
}
return $price;
}
public function calculatePrice($quantity, $pricePerUnit): float {
if ($quantity <= 0) {
return 0;
}
$price = $quantity * $pricePerUnit;
if ($price > 100) {
//...
}
return $price;
}
public function calculateTax($price, $taxRate): int {
$tax = 0;
if ($price > 0) {
$tax = $price * $taxRate;
}
return $tax;
}
public function calculateTax($price, $taxRate): int {
if ($price <= 0) {
return 0;
}
return $price * $taxRate;
}
if ($user->isAdmin()) {
if ($user->isSuperAdmin()) {
// do something
}
else {
// do something else
}
}
else {
// do something different
}
if ($user->isSuperAdmin()) {
// do something
}
else if ($user->isAdmin()) {
// do something else
}
else {
// do something different
}
public function addNumbers($a, $b): int {
$result = $a + $b;
$finalResult = $result * 2; // Useless variable
return $finalResult;
}
public function addNumbers($a, $b): int {
return ($a + $b) * 2; // Directly use the expression
}
if (!empty($user)) {
return $user;
}
return false;
return $user ?? false;
$a = '7';
$b = 7;
if ($a != $b) {
// The expression will always pass
}
$a = '7';
$b = 7;
if ($a !== $b) {
// The expression is verified
}
if (null === $variable) {
// code here
}
if ($variable = null) {
}
if ($article->status === 'published') {
// ...
}
if ($article->isPublished()) {
// ...
}
private function withOrderFromUri(\Mockery\MockInterface $someService, SomeOtherClass $otherClass, Order $order): void {
$someService->expects()->getOrderByUri($otherClass->getResource()->getUri())->andReturns($order);
}
private function withOrderFromUri(
\Mockery\MockInterface $someService,
SomeOtherClass $otherClass,
Order $order,
): void {
$someService
->expects()
->getOrderByUri($otherClass->getResource()->getUri())
->andReturns($order)
;
}
// Set the billing address for an invoice
$invoice->setBillingAddress(
$order->getCustomer()
?->getAddress()
?->getBillingAddress()
);
private function doSomething(Some $thing, string $callerId): void
{
$this->logger->info(
'Something really important happented within {something}, triggered'
. 'from caller {callerId}',
[
'something' => $thing->getName(),
'callerId' => $callerId,
],
);
}
private function doSomething(Some $thing, string $callerId): void
{
$this->logger->info(
'Something really important happented within {something}, triggered from caller {callerId}',
[
'something' => $thing->getName(),
'callerId' => $callerId,
],
);
}
public function testCalculateTotalPriceForArticles(): void
{
$taxService = \Mockery::mock(TaxServiceInterface::class);
$taxService->expects()->getTaxRate()->andReturns(0.19);
$logger = \Mockery::mock(LoggerInterface::class);
$logger
->expects()
->info(
'A total price {totalPrice} has been calculated, including a tax portion of {taxRate}%',
[
'totalPrice' => 51.1581,
'taxRate' => 0.19,
],
)
;
$this->assertEquals(
51.1581,
(new PriceCalculationService($taxService, $logger))
->calculateTotalPriceForArticles(
[
(new Article)->setPrice(10.00),
(new Article)->setPrice(10.90),
(new Article)->setPrice(10.09),
(new Article)->setPrice(2.00),
],
),
);
}
public function testCalculateTotalPriceForArticles(): void
{
$taxService = \Mockery::mock(TaxServiceInterface::class);
$taxService->expects()->getTaxRate()->andReturns(0.19);
$logger = \Mockery::mock(LoggerInterface::class);
$logger
->expects()
->info(
'A total price {totalPrice} has been calculated, including a tax portion of {taxRate}%',
[
'totalPrice' => 51.1581,
'taxRate' => 0.19,
],
)
;
$service = new PriceCalculationService($taxService, $logger);
$articles = [
(new Article)->setPrice(10.00),
(new Article)->setPrice(10.90),
(new Article)->setPrice(10.09),
(new Article)->setPrice(2.00),
];
$actual = $service->calculateTotalPriceForArticles($articles);
$this->assertEquals(51.1581, $actual);
}
public function testCalculateTotalPriceForArticles(): void
{
$taxService = \Mockery::mock(TaxServiceInterface::class);
$this->withTaxRate($taxService);
$logger = \Mockery::mock(LoggerInterface::class);
$this->expectsPriceCalculatedInfoLog($logger);
$service = new PriceCalculationService($taxService, $logger);
$articles = [
(new Article)->setPrice(10.00),
(new Article)->setPrice(10.90),
(new Article)->setPrice(10.09),
(new Article)->setPrice(2.00),
];
$actual = $service->calculateTotalPriceForArticles($articles);
$this->assertEquals(51.1581, $actual);
}
private function withTaxRate(\Mockery\MockInterface $taxService): void
{
$taxService->expects()->getTaxRate()->andReturns(0.19);
}
private function expectsPriceCalculatedInfoLog(
\Mockery\MockInterface $logger,
): void {
$logger
->expects()
->info(
'A total price {totalPrice} has been calculated, including a tax portion of {taxRate}%',
[
'totalPrice' => 51.1581,
'taxRate' => 0.19,
],
)
;
}