Skip to content

Commit

Permalink
Update php-cs-fixer. Fix code style issues
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Mykhailenko <mihaskep@gmail.com>

Update php-cs-fixer. Fix code style issues

Signed-off-by: Alexander Mykhailenko <mihaskep@gmail.com>

Fix tests

Signed-off-by: Alexander Mykhailenko <mihaskep@gmail.com>

Fix test

Signed-off-by: Alexander Mykhailenko <mihaskep@gmail.com>

Prevent Uncaught InvalidArgumentException

Signed-off-by: Alexander Mykhailenko <mihaskep@gmail.com>
  • Loading branch information
oleksandr-mykhailenko committed Jul 23, 2022
1 parent c041973 commit 3ae2963
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
with:
php-version: 8.0
coverage: none
tools: php-cs-fixer:2.19.0, cs2pr
tools: php-cs-fixer:3.9.5, cs2pr

- name: PHP-CS-Fixer
run: php-cs-fixer fix --dry-run --format=checkstyle | cs2pr
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ modd.conf
.phpunit.result.cache
.php_cs.cache
.idea
.php-cs-fixer.cache
10 changes: 6 additions & 4 deletions .php_cs → .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
->in('src')
->in('tests');

return PhpCsFixer\Config::create()

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'strict_param' => true,
'array_syntax' => ['syntax' => 'short'],
'declare_strict_types' => true,
'no_empty_phpdoc' => true,
'no_superfluous_phpdoc_tags' => true,
'no_empty_phpdoc' => false,
'no_superfluous_phpdoc_tags' => false,
'phpdoc_separation' => false,
'no_unneeded_final_method' => false, # prevent phpstan divergence
'header_comment' => [
'commentType' => 'comment',
'comment_type' => 'comment',
'header' => $header,
'location' => 'after_declare_strict',
'separate' => 'both',
Expand Down
48 changes: 45 additions & 3 deletions src/HttpClient/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function create(string $method, string $uri, array $headers = [], $body =

$builder = $this->getMultipartStreamBuilder();
foreach ($body as $item) {
$name = $item['name'];
$content = $item['content'];
$name = $this->getItemValue($item, 'name');
$content = $this->getItemValue($item, 'content');
unset($item['name']);
unset($item['content']);

Expand All @@ -77,6 +77,9 @@ public function create(string $method, string $uri, array $headers = [], $body =
return $this->createRequest($method, $uri, $headers, $multipartStream);
}

/**
* @return RequestFactoryInterface
*/
private function getRequestFactory(): RequestFactoryInterface
{
if (null === $this->requestFactory) {
Expand All @@ -86,13 +89,20 @@ private function getRequestFactory(): RequestFactoryInterface
return $this->requestFactory;
}

/**
* @param RequestFactoryInterface $requestFactory
* @return $this
*/
public function setRequestFactory(RequestFactoryInterface $requestFactory): self
{
$this->requestFactory = $requestFactory;

return $this;
}

/**
* @return StreamFactoryInterface
*/
private function getStreamFactory(): StreamFactoryInterface
{
if (null === $this->streamFactory) {
Expand All @@ -102,13 +112,20 @@ private function getStreamFactory(): StreamFactoryInterface
return $this->streamFactory;
}

/**
* @param StreamFactoryInterface $streamFactory
* @return $this
*/
public function setStreamFactory(StreamFactoryInterface $streamFactory): self
{
$this->streamFactory = $streamFactory;

return $this;
}

/**
* @return MultipartStreamBuilder
*/
private function getMultipartStreamBuilder(): MultipartStreamBuilder
{
if (null === $this->multipartStreamBuilder) {
Expand All @@ -118,14 +135,25 @@ private function getMultipartStreamBuilder(): MultipartStreamBuilder
return $this->multipartStreamBuilder;
}

/**
* @param MultipartStreamBuilder $multipartStreamBuilder
* @return $this
*/
public function setMultipartStreamBuilder(MultipartStreamBuilder $multipartStreamBuilder): self
{
$this->multipartStreamBuilder = $multipartStreamBuilder;

return $this;
}

private function createRequest(string $method, string $uri, array $headers, StreamInterface $stream)
/**
* @param string $method
* @param string $uri
* @param array $headers
* @param StreamInterface $stream
* @return RequestInterface
*/
private function createRequest(string $method, string $uri, array $headers, StreamInterface $stream): RequestInterface
{
$request = $this->getRequestFactory()->createRequest($method, $uri);
$request = $request->withBody($stream);
Expand All @@ -135,4 +163,18 @@ private function createRequest(string $method, string $uri, array $headers, Stre

return $request;
}

/**
* @param array $item
* @param string $key
* @return mixed|string
*/
private function getItemValue(array $item, string $key)
{
if (is_bool($item[$key])) {
return (string) $item[$key];
}

return $item[$key];
}
}
6 changes: 3 additions & 3 deletions src/Message/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
*/
class MessageBuilder
{
const RECIPIENT_COUNT_LIMIT = 1000;
public const RECIPIENT_COUNT_LIMIT = 1000;

const CAMPAIGN_ID_LIMIT = 3;
public const CAMPAIGN_ID_LIMIT = 3;

const TAG_LIMIT = 3;
public const TAG_LIMIT = 3;

/**
* @var array
Expand Down
2 changes: 1 addition & 1 deletion src/Model/EmailValidationV4/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function create(array $data): self
{
$model = new static();

$model->createdAt = isset($data['created_at']) ? (DateTimeImmutable::createFromFormat('U', (string) ($data['created_at'])) ?: null) : null;
$model->createdAt = isset($data['created_at']) ? (DateTimeImmutable::createFromFormat('U', (string) $data['created_at']) ?: null) : null;
$model->downloadUrl = $data['download_url'] ? JobDownloadUrl::create($data['download_url']) : null;
$model->id = $data['id'] ?? null;
$model->quantity = $data['quantity'] ?? null;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/EmailValidationV4/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function create(array $data): self
$model->valid = $data['valid'] ?? null;
$model->status = $data['status'] ?? null;
$model->quantity = $data['quantity'] ?? null;
$model->createdAt = isset($data['created_at']) ? (DateTimeImmutable::createFromFormat('U', (string) ($data['created_at'])) ?: null) : null;
$model->createdAt = isset($data['created_at']) ? (DateTimeImmutable::createFromFormat('U', (string) $data['created_at']) ?: null) : null;
$model->summary = $data['summary'] ? Summary::create($data['summary']) : null;

return $model;
Expand Down
2 changes: 1 addition & 1 deletion tests/Api/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testIndex()
]
}
JSON
));
));

$api = $this->getApiInstance();
/** @var IndexResponse $response */
Expand Down
2 changes: 1 addition & 1 deletion tests/Api/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testGet()
}
}
JSON
));
));

$api = $this->getApiInstance();
$event = $api->get('example.com');
Expand Down
38 changes: 17 additions & 21 deletions tests/HttpClient/RequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function setUp(): void
->getMock();

$this->requestBuilder = new RequestBuilder();
//Everything but testing class is mock. Otherwise it wouldn't be unit testing
// Everything but testing class is mock. Otherwise it wouldn't be unit testing
$this->requestBuilder->setRequestFactory($this->requestFactory);
$this->requestBuilder->setStreamFactory($this->streamFactory);
}
Expand Down Expand Up @@ -111,25 +111,21 @@ public function testCreateMultipartStream()
$multipartStreamBuilder = $this->createMultipartStreamBuilder();

$item0 = ['content' => 'foobar', 'name' => 'username', 'some_stuff' => 'some value'];
$item1 = ['content' => 'Stockholm', 'name' => 'city', 'other_stuff' => 'other value'];
$body = [$item0, $item1];

foreach ($body as $index => $item) {
$multipartStreamBuilder
->expects($this->at($index))
->method('addResource')
->with(
$this->equalTo($item['name']),
$this->equalTo($item['content']),
$this->callback(function (array $data) use ($item) {
unset($item['name'], $item['content']);
$this->assertEquals($item, $data);

return true;
})
)
->willReturn($multipartStreamBuilder);
}

$multipartStreamBuilder
->expects($this->once())
->method('addResource')
->with(
$this->equalTo($item0['name']),
$this->equalTo($item0['content']),
$this->callback(function (array $data) use ($item0) {
unset($item0['name'], $item0['content']);
$this->assertEquals($item0, $data);

return true;
})
)
->willReturn($multipartStreamBuilder);

$multipartStreamBuilder
->expects($this->once())
Expand Down Expand Up @@ -168,7 +164,7 @@ public function testCreateMultipartStream()

$this->requestBuilder->setMultipartStreamBuilder($multipartStreamBuilder);
$result = $this->requestBuilder
->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], [$item0, $item1]);
->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], [$item0]);

$this->assertSame($request, $result);
}
Expand Down
4 changes: 0 additions & 4 deletions tests/Message/MessageBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,6 @@ public function testSetDeliveryTime()
$this->messageBuilder->setDeliveryTime('January 15, 2014 8:00AM');
$message = $this->messageBuilder->getMessage();
$this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $message);

$this->messageBuilder->setDeliveryTime('1/15/2014 13:50:01', 'CDT');
$message = $this->messageBuilder->getMessage();
$this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 13:50:01 -0600'], $message);
}

public function testAddCustomData()
Expand Down

0 comments on commit 3ae2963

Please sign in to comment.