Skip to content

Commit

Permalink
PHPCS - fixies
Browse files Browse the repository at this point in the history
  • Loading branch information
pekral committed Oct 13, 2020
1 parent ce9f420 commit c76f3ab
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 178 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ $evidenceData['nazev'] = 'První kontaktní adresa'
$evidenceItemId = $client->save($evidenceData, null, $dryRun, $uriParameters);
```
Pokud vše proběhne v pořádku, vratí se třída `\EcomailFlexibee\Http\Response\Response:class` s daty ze systému Flexibee. Nastane-li chyba, vyhodí se výjimka
`EcomailFlexibeeRequestError::class`. Pro editaci záznamu stačí vyplnit druhý parametr `$id`.
`EcomailFlexibeeRequestFail::class`. Pro editaci záznamu stačí vyplnit druhý parametr `$id`.

## Vrácení záznamu dle parametrů
Nalezení záznamu dle id s vyhozením výjimky, pokud záznam neexistuje
Expand Down
59 changes: 36 additions & 23 deletions ruleset.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?xml version="1.0"?>
<ruleset name="EcomailFlexiBeeClientProject">
<ruleset name="Ecomail - FlexiBee client">
<!-- Base PHPCS configuration -->
<config name="installed_paths" value="../../slevomat/coding-standard"/>
<arg name="extensions" value="php,inc" />
<ini name="memory_limit" value="8G"/>
<arg name="extensions" value="php" />
<arg name="encoding" value="utf-8" />
<arg name="colors"/>
<arg name="tab-width" value="4"/>

<exclude-pattern>/resources</exclude-pattern>

<!-- Enabled rules with configuration-->
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
<properties>
Expand All @@ -20,7 +23,11 @@
<property name="spacesCountAroundEqualsSign" value="1"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
<properties>
<property name="enableNativeTypeHint" value="false"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<properties>
<property name="enableObjectTypeHint" value="false"/>
Expand Down Expand Up @@ -110,6 +117,7 @@
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHintSpacing"/>
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameAfterKeyword"/>
<rule ref="SlevomatCodingStandard.Namespaces.MultipleUsesPerLine"/>
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
<rule ref="SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces"/>
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation"/>
Expand All @@ -126,8 +134,6 @@
<rule ref="SlevomatCodingStandard.ControlStructures.UselessTernaryOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountAfterControlStructure"/>
<rule ref="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountBeforeControlStructure"/>
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants"/>
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireMultiLineTernaryOperator">
<properties>
<property name="lineLengthLimit" value="75"/>
Expand All @@ -139,28 +145,35 @@
<property name="maxLineLength" value="150"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition">
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>

<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="ignoreAssignmentsInsideFunctionCalls" value="true"/>
<property name="rootNamespaces" type="array">
<element key="src" value="EcomailFlexibee"/>
<element key="tests" value="EcomailFlexibeeTest"/>
</property>
</properties>
</rule>

<!-- Disabled sniffs (try enable and do refactoring) -->
<exclude name="SlevomatCodingStandard.PHP.RequireExplicitAssertion"/>
<exclude name="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint"/>
<exclude name="SlevomatCodingStandard.Namespaces.UseOnlyWhitelistedNamespaces"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedExceptions"/>
<exclude name="SlevomatCodingStandard.Files.TypeNameMatchesFileName"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireYodaComparison"/>
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLinePropertyDocComment"/>
<exclude name="SlevomatCodingStandard.ControlStructures.NewWithoutParentheses"/>
<exclude name="SlevomatCodingStandard.Files.LineLength"/>
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLineDocComment"/>
<exclude name="SlevomatCodingStandard.ControlStructures.DisallowShortTernaryOperator"/>
<exclude name="SlevomatCodingStandard.Classes.DisallowMultiPropertyDefinition"/>
<exclude name="SlevomatCodingStandard.Classes.DisallowMultiConstantDefinition"/>
<rule ref="SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable"/>
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses.UselessParentheses"/>
<rule ref="SlevomatCodingStandard.Functions.RequireArrowFunction"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator"/>
<rule ref="SlevomatCodingStandard.Functions.RequireMultiLineCall">
<properties>
<property name="minLineLength" value="125"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Functions.RequireSingleLineCall">
<properties>
<property name="maxLineLength" value="124"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireMultiLineCondition">
<properties>
<property name="minLineLength" value="125"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Classes.SuperfluousErrorNaming"/>
<rule ref="SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration"/>

</ruleset>
75 changes: 44 additions & 31 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
use EcomailFlexibee\Http\ResponseHydrator;
use EcomailFlexibee\Http\UrlBuilder;
use EcomailFlexibee\Result\EvidenceResult;
use function array_filter;
use function array_merge;
use function count;
use function mb_strtolower;
use function sprintf;

class Client
{

protected \EcomailFlexibee\Http\UrlBuilder $queryBuilder;
protected \EcomailFlexibee\Http\HttpClient $httpClient;
protected \EcomailFlexibee\Config $config;
protected UrlBuilder $queryBuilder;
protected HttpClient $httpClient;
protected Config $config;

private \EcomailFlexibee\Http\ResponseHydrator $responseHydrator;
private ResponseHydrator $responseHydrator;

public function __construct(
string $url,
Expand Down Expand Up @@ -112,7 +117,10 @@ public function getCompanies(): Response
*/
public function getCompany(): array
{
$result = \array_filter($this->getCompanies()->getData()['companies'], fn (array $data): bool => \mb_strtolower($data['dbNazev']) === \mb_strtolower($this->config->getCompany()));
$result = array_filter(
$this->getCompanies()->getData()['companies'],
fn (array $data): bool => mb_strtolower($data['dbNazev']) === mb_strtolower($this->config->getCompany()),
);

if (isset($result['company'])) {
return $result['company'];
Expand Down Expand Up @@ -164,7 +172,7 @@ public function deleteByCode(string $id, bool $dryRun = false): void
{
$uriParameters = $dryRun ? ['dry-run' => 'true'] : [];
$this->httpClient->request(
$this->queryBuilder->createUri(\sprintf('code:%s', $id), $uriParameters),
$this->queryBuilder->createUri(sprintf('code:%s', $id), $uriParameters),
Method::get(Method::DELETE),
[],
[],
Expand All @@ -175,12 +183,12 @@ public function deleteByCode(string $id, bool $dryRun = false): void

/**
* @param array<mixed> $uriParameters
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function findById(int $id, array $uriParameters = []): EvidenceResult
{
Expand All @@ -193,14 +201,14 @@ public function findById(int $id, array $uriParameters = []): EvidenceResult

/**
* @param array<mixed> $uriParameters
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidRequestParameter
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNoEvidenceResult
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function getByCode(string $code, array $uriParameters = []): EvidenceResult
{
Expand All @@ -219,13 +227,13 @@ public function getByCode(string $code, array $uriParameters = []): EvidenceResu

/**
* @param array<string> $uriParameters
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNoEvidenceResult
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function getById(int $id, array $uriParameters = []): EvidenceResult
{
Expand All @@ -244,13 +252,13 @@ public function getById(int $id, array $uriParameters = []): EvidenceResult

/**
* @param array<mixed> $uriParameters
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidRequestParameter
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function findByCode(string $code, array $uriParameters = []): EvidenceResult
{
Expand All @@ -264,12 +272,12 @@ public function findByCode(string $code, array $uriParameters = []): EvidenceRes
/**
* @param array<mixed> $evidenceData
* @param array<mixed> $uriParameters
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeSaveFailed
*/
public function save(array $evidenceData, ?int $id, bool $dryRun = false, array $uriParameters = []): FlexibeeResponse
Expand All @@ -281,14 +289,19 @@ public function save(array $evidenceData, ?int $id, bool $dryRun = false, array
$postData = [];
$postData[$this->config->getEvidence()] = $evidenceData;
$uriParameters = $dryRun
? \array_merge($uriParameters, ['dry-run' => 'true'])
? array_merge($uriParameters, ['dry-run' => 'true'])
: $uriParameters;
/** @var \EcomailFlexibee\Result\EvidenceResult $response */
$response = $this->callRequest(Method::get(Method::PUT), null, $uriParameters, $postData, [])[0];
$data = $response->getData();

if (isset($data['created']) && (int) $data['created'] === 0 && isset($data['updated']) && (int) $data['updated'] === 0) {
$errorMessage = \sprintf('(%d) %s', $data['status_code'], $data['message']);
if (
isset($data['created'])
&& (int) $data['created'] === 0
&& isset($data['updated'])
&& (int) $data['updated'] === 0
) {
$errorMessage = sprintf('(%d) %s', $data['status_code'], $data['message']);

throw new EcomailFlexibeeSaveFailed($errorMessage);
}
Expand All @@ -302,7 +315,7 @@ public function save(array $evidenceData, ?int $id, bool $dryRun = false, array
null,
true,
null,
\count($data),
count($data),
null,
$response->getData(),
[],
Expand All @@ -327,7 +340,7 @@ public function addUserRelation(int $objectAId, int $objectBId, float $price, in
'cena' => $price,
'popis' => $description,
'evidenceType' => $this->config->getEvidence(),
'object' => \sprintf('code:%s', $objectBData['kod']),
'object' => sprintf('code:%s', $objectBData['kod']),
],
],
];
Expand All @@ -337,12 +350,12 @@ public function addUserRelation(int $objectAId, int $objectBId, float $price, in

/**
* @return array<mixed>
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function allInEvidence(): array
{
Expand Down Expand Up @@ -374,12 +387,12 @@ public function countInEvidence(): int

/**
* @return array<\EcomailFlexibee\Result\EvidenceResult>
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function chunkInEvidence(int $start, int $limit): array
{
Expand All @@ -398,12 +411,12 @@ public function chunkInEvidence(int $start, int $limit): array
/**
* @param array<string> $uriParameters
* @return array<\EcomailFlexibee\Result\EvidenceResult>
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function searchInEvidence(string $query, array $uriParameters): array
{
Expand All @@ -425,12 +438,12 @@ public function searchInEvidence(string $query, array $uriParameters): array
* @param array<mixed> $postFields
* @param array<string> $headers
* @return array<\EcomailFlexibee\Result\EvidenceResult>
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function callRequest(Method $httpMethod, $queryFilterOrId, array $uriParameters, array $postFields, array $headers): array
{
Expand All @@ -448,12 +461,12 @@ public function callRequest(Method $httpMethod, $queryFilterOrId, array $uriPara

/**
* @param array<mixed> $uriParameters
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeConnectionFail
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeForbidden
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeMethodNotAllowed
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeNotAcceptableRequest
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestFail
*/
public function getPdfById(int $id, array $uriParameters): Response
{
Expand Down
22 changes: 15 additions & 7 deletions src/Enum/SearchQueryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
namespace EcomailFlexibee\Enum;

use Consistence\Enum\Enum;
use function array_keys;
use function array_values;
use function implode;
use function preg_match_all;
use function preg_replace;
use function preg_split;
use function str_replace;
use function urldecode;

class SearchQueryOperator extends Enum
{
Expand All @@ -23,24 +31,24 @@ class SearchQueryOperator extends Enum

public static function convertOperatorsInQuery(string $query): string
{
$query = \urldecode($query);
$query = urldecode($query);
/** @var array<mixed> $queryExploded */
$queryExploded = \preg_split('/\s+/', $query);
$queryExploded = preg_split('/\s+/', $query);

foreach($queryExploded as &$part) {
$toReplace = [];
\preg_match_all('/[^\'](?=(?:[^\']*\'[^"]*\'[^\']*|[^\'])*$)/', $part, $matches);
preg_match_all('/[^\'](?=(?:[^\']*\'[^"]*\'[^\']*|[^\'])*$)/', $part, $matches);

if (isset($matches[0])) {
$text = \implode('', $matches[0]);
$toReplace[$text] = \str_replace(\array_keys(self::$operators), \array_values(self::$operators), $text);
$text = implode('', $matches[0]);
$toReplace[$text] = str_replace(array_keys(self::$operators), array_values(self::$operators), $text);
}

$part = \str_replace(\array_keys($toReplace), \array_values($toReplace), $part);
$part = str_replace(array_keys($toReplace), array_values($toReplace), $part);
}

/** @var string $result */
$result = \preg_replace('/\s+/', ' ', \implode(' ',$queryExploded));
$result = preg_replace('/\s+/', ' ', implode(' ',$queryExploded));

return $result;
}
Expand Down
Loading

0 comments on commit c76f3ab

Please sign in to comment.