Skip to content

Commit a61873e

Browse files
authored
Add initial version (#1)
* Add initial version of the Notification * Update change log * Update .scrutinizer.yml
1 parent 79cf637 commit a61873e

File tree

7 files changed

+367
-18
lines changed

7 files changed

+367
-18
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ max_line_length = 120
1414
[*.{diff,md}]
1515
trim_trailing_whitespace = false
1616

17-
[*.{html,js,rb,scss,xml,yml,yaml,json}]
17+
[*.{html,js,rb,scss,xml,yml,yaml,json,yaml,yml}]
1818
indent_size = 2
1919

2020
[*.txt]

.scrutinizer.yml

Lines changed: 98 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,104 @@
11
filter:
2-
excluded_paths: [tests/*]
2+
excluded_paths:
3+
- build/*
4+
- tests/*
5+
- vendor/*
36

47
checks:
5-
php:
6-
remove_extra_empty_lines: true
7-
remove_php_closing_tag: true
8-
remove_trailing_whitespace: true
9-
fix_use_statements:
10-
remove_unused: true
11-
preserve_multiple: false
12-
preserve_blanklines: true
13-
order_alphabetically: true
14-
fix_php_opening_tag: true
15-
fix_linefeed: true
16-
fix_line_ending: true
17-
fix_identation_4spaces: true
18-
fix_doc_comments: true
19-
code_rating: true
20-
duplication: true
8+
php:
9+
verify_property_names: true
10+
verify_argument_usable_as_reference: true
11+
verify_access_scope_valid: true
12+
variable_existence: true
13+
useless_calls: true
14+
use_statement_alias_conflict: true
15+
unused_variables: true
16+
unused_properties: true
17+
unused_parameters: true
18+
unused_methods: true
19+
unreachable_code: true
20+
too_many_arguments: true
21+
symfony_request_injection: false
22+
switch_fallthrough_commented: true
23+
sql_injection_vulnerabilities: true
24+
simplify_boolean_return: true
25+
security_vulnerabilities: true
26+
return_in_constructor: true
27+
return_doc_comments: true
28+
return_doc_comment_if_not_inferrable: true
29+
require_scope_for_methods: true
30+
require_php_tag_first: true
31+
remove_extra_empty_lines: true
32+
property_assignments: true
33+
properties_in_camelcaps: true
34+
precedence_mistakes: true
35+
precedence_in_conditions: true
36+
phpunit_assertions: true
37+
parse_doc_comments: true
38+
parameters_in_camelcaps: true
39+
parameter_non_unique: true
40+
parameter_doc_comments: true
41+
param_doc_comment_if_not_inferrable: true
42+
overriding_private_members: true
43+
overriding_parameter: true
44+
non_commented_empty_catch_block: true
45+
no_trait_type_hints: true
46+
no_trailing_whitespace: true
47+
no_short_variable_names:
48+
minimum: '3'
49+
no_short_open_tag: true
50+
no_short_method_names:
51+
minimum: '3'
52+
no_property_on_interface: true
53+
no_non_implemented_abstract_methods: true
54+
no_long_variable_names:
55+
maximum: '20'
56+
no_goto: true
57+
no_exit: true
58+
no_eval: true
59+
no_error_suppression: true
60+
no_debug_code: true
61+
more_specific_types_in_doc_comments: true
62+
missing_arguments: true
63+
method_calls_on_non_object: true
64+
instanceof_class_exists: true
65+
foreach_usable_as_reference: true
66+
foreach_traversable: true
67+
fix_use_statements:
68+
remove_unused: true
69+
preserve_multiple: false
70+
preserve_blanklines: false
71+
order_alphabetically: true
72+
fix_line_ending: true
73+
fix_doc_comments: true
74+
encourage_shallow_comparison: true
75+
duplication: true
76+
deprecated_code_usage: true
77+
deadlock_detection_in_loops: true
78+
comparison_always_same_result: true
79+
code_rating: true
80+
closure_use_not_conflicting: true
81+
closure_use_modifiable: true
82+
check_method_contracts:
83+
verify_interface_like_constraints: true
84+
verify_documented_constraints: true
85+
verify_parent_constraints: true
86+
catch_class_exists: true
87+
call_to_parent_method: true
88+
avoid_superglobals: true
89+
avoid_length_functions_in_loops: true
90+
avoid_entity_manager_injection: true
91+
avoid_duplicate_types: true
92+
avoid_closing_tag: true
93+
assignment_of_null_return: true
94+
argument_type_checks: true
95+
96+
build:
97+
nodes:
98+
analysis:
99+
tests:
100+
override:
101+
- php-scrutinizer-run
21102

22103
tools:
23104
external_code_coverage:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ Security - in case of vulnerabilities.
1717
## [Unreleased]
1818

1919
_TBD_
20+
21+
## [0.1.0] 2018-05-26
22+
23+
Initial release.

src/Error.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* This file is part of Zee Project.
4+
*
5+
* @see https://github.com/zee/
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zee\Errors;
11+
12+
/**
13+
* Class Error.
14+
*/
15+
final class Error
16+
{
17+
/**
18+
* @var string
19+
*/
20+
private $message;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $context;
26+
27+
/**
28+
* @param string $message
29+
* @param array $context
30+
*/
31+
public function __construct(string $message, array $context)
32+
{
33+
$this->message = $message;
34+
$this->context = $context;
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function __toString(): string
41+
{
42+
return $this->getMessage();
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
public function getMessage(): string
49+
{
50+
return $this->message;
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function getContext(): array
57+
{
58+
return $this->context;
59+
}
60+
}

src/Notification.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* This file is part of Zee Project.
4+
*
5+
* @see https://github.com/zee/
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zee\Errors;
11+
12+
use ArrayIterator;
13+
use Countable;
14+
use IteratorAggregate;
15+
use Traversable;
16+
17+
/**
18+
* Class Notification.
19+
*/
20+
final class Notification implements Countable, IteratorAggregate
21+
{
22+
private $errors = [];
23+
24+
/**
25+
* Adds new error to the notification.
26+
*
27+
* @param string $message
28+
* @param array $context
29+
*/
30+
public function addError(string $message, array $context = []): void
31+
{
32+
$this->errors[] = new Error($message, $context);
33+
}
34+
35+
/**
36+
* Returns whether the notification has any errors.
37+
*
38+
* @return bool
39+
*/
40+
public function hasErrors(): bool
41+
{
42+
return !empty($this->errors);
43+
}
44+
45+
/**
46+
* Clear the notification errors.
47+
*/
48+
public function clearErrors(): void
49+
{
50+
$this->errors = [];
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function getErrorMessages(): array
57+
{
58+
return array_map(
59+
function (Error $error) {
60+
return $error->getMessage();
61+
},
62+
$this->errors
63+
);
64+
}
65+
66+
/**
67+
* Counts the errors.
68+
*
69+
* @inheritdoc
70+
*/
71+
public function count(): int
72+
{
73+
return count($this->errors);
74+
}
75+
76+
/**
77+
* Builds the iterator by errors.
78+
*
79+
* @inheritdoc
80+
*
81+
* @return Traversable|Error[]
82+
*/
83+
public function getIterator(): Traversable
84+
{
85+
return new ArrayIterator($this->errors);
86+
}
87+
}

tests/ErrorTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* This file is part of Zee Project.
4+
*
5+
* @see https://github.com/zee/
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zee\Errors\Tests;
11+
12+
use Zee\Errors\Error;
13+
14+
/**
15+
* Class ErrorTest.
16+
*/
17+
final class ErrorTest extends TestCase
18+
{
19+
/**
20+
* @test
21+
*
22+
* @return Error
23+
*/
24+
public function errorContainsMessageAndItsContextData(): Error
25+
{
26+
$error = new Error('Something went wrong', ['foo' => 'bar']);
27+
28+
self::assertSame('Something went wrong', $error->getMessage());
29+
self::assertSame(['foo' => 'bar'], $error->getContext());
30+
31+
return $error;
32+
}
33+
34+
/**
35+
* @test
36+
* @depends errorContainsMessageAndItsContextData
37+
*
38+
* @param Error $error
39+
*/
40+
public function canBeCastedToString(Error $error)
41+
{
42+
self::assertSame('Something went wrong', (string) $error);
43+
}
44+
}

0 commit comments

Comments
 (0)