Skip to content

Commit 7e876d2

Browse files
authored
Merge pull request #6 from Riimu/next
Added support for class resolution operator strings
2 parents 48e1a30 + 54d55bd commit 7e876d2

23 files changed

+315
-132
lines changed

.php_cs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,115 @@ return \PhpCsFixer\Config::create()
88
->setRules([
99
'@PSR2' => true,
1010

11-
'array_syntax' => [
12-
'syntax' => 'short'
13-
],
14-
'binary_operator_spaces' => [
15-
'align_double_arrow' => null,
16-
'align_equals' => false,
17-
],
11+
'align_multiline_comment' => true,
12+
'array_indentation' => true,
13+
'array_syntax' => ['syntax' => 'short'],
14+
'backtick_to_shell_exec' => true,
15+
'binary_operator_spaces' => true,
1816
'blank_line_after_opening_tag' => true,
1917
'cast_spaces' => true,
18+
'class_attributes_separation' => ['elements' => ['method']],
19+
'combine_consecutive_issets' => true,
2020
'combine_consecutive_unsets' => true,
21-
'concat_space' => [
22-
'spacing' => 'one'
23-
],
21+
'concat_space' => ['spacing' => 'one'],
2422
'declare_equal_normalize' => true,
2523
'dir_constant' => true,
2624
'ereg_to_preg' => true,
25+
'fully_qualified_strict_types' => true,
2726
'function_to_constant' => true,
2827
'function_typehint_space' => true,
29-
'hash_to_slash_comment' => true,
3028
'heredoc_to_nowdoc' => true,
3129
'include' => true,
3230
'is_null' => true,
31+
'list_syntax' => ['syntax' => 'short'],
32+
'logical_operators' => true,
3333
'lowercase_cast' => true,
34+
'lowercase_static_reference' => true,
3435
'magic_constant_casing' => true,
35-
'method_separation' => true,
3636
'modernize_types_casting' => true,
37+
'multiline_comment_opening_closing' => true,
38+
'multiline_whitespace_before_semicolons' => true,
39+
'native_constant_invocation' => true,
3740
'native_function_casing' => true,
41+
'native_function_invocation' => ['include' => ['@compiler_optimized']],
3842
'new_with_braces' => true,
3943
'no_alias_functions' => true,
44+
'no_binary_string' => true,
4045
'no_blank_lines_after_class_opening' => true,
4146
'no_blank_lines_after_phpdoc' => true,
4247
'no_empty_comment' => true,
4348
'no_empty_phpdoc' => true,
4449
'no_empty_statement' => true,
45-
'no_extra_consecutive_blank_lines' => true,
50+
'no_extra_blank_lines' => true,
51+
'no_homoglyph_names' => true,
4652
'no_leading_import_slash' => true,
4753
'no_leading_namespace_whitespace' => true,
4854
'no_mixed_echo_print' => true,
4955
'no_multiline_whitespace_around_double_arrow' => true,
50-
'no_multiline_whitespace_before_semicolons' => true,
5156
'no_php4_constructor' => true,
5257
'no_short_bool_cast' => true,
5358
'no_singleline_whitespace_before_semicolons' => true,
5459
'no_spaces_around_offset' => true,
5560
'no_trailing_comma_in_list_call' => true,
5661
'no_trailing_comma_in_singleline_array' => true,
5762
'no_unneeded_control_parentheses' => true,
63+
'no_unneeded_curly_braces' => true,
64+
'no_unneeded_final_method' => true,
65+
'no_unreachable_default_argument_value' => true,
5866
'no_unused_imports' => true,
5967
'no_useless_return' => true,
6068
'no_whitespace_before_comma_in_array' => true,
6169
'no_whitespace_in_blank_line' => true,
6270
'non_printable_character' => true,
6371
'normalize_index_brace' => true,
6472
'object_operator_without_whitespace' => true,
65-
'ordered_class_elements' => [
66-
'order' => ['use_trait', 'constant', 'property', 'construct', 'method'],
67-
],
73+
'ordered_class_elements' => ['order' => ['use_trait', 'constant', 'property', 'construct', 'method']],
6874
'ordered_imports' => true,
6975
'php_unit_construct' => true,
70-
'php_unit_dedicate_assert' => true,
76+
'php_unit_dedicate_assert' => ['target' => 'newest'],
77+
'php_unit_expectation' => true,
78+
'php_unit_mock' => true,
79+
'php_unit_namespaced' => true,
80+
'php_unit_no_expectation_annotation' => true,
81+
'php_unit_set_up_tear_down_visibility' => true,
7182
'php_unit_strict' => true,
83+
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
7284
'phpdoc_add_missing_param_annotation' => true,
85+
'phpdoc_align' => ['align' => 'left'],
7386
'phpdoc_annotation_without_dot' => true,
7487
'phpdoc_indent' => true,
7588
'phpdoc_inline_tag' => true,
7689
'phpdoc_no_access' => true,
7790
'phpdoc_no_alias_tag' => true,
7891
'phpdoc_no_package' => true,
92+
'phpdoc_no_useless_inheritdoc' => true,
93+
'phpdoc_return_self_reference' => true,
7994
'phpdoc_scalar' => true,
8095
'phpdoc_single_line_var_spacing' => true,
8196
'phpdoc_summary' => true,
97+
'phpdoc_to_comment' => true,
8298
'phpdoc_trim' => true,
99+
'phpdoc_trim_consecutive_blank_line_separation' => true,
83100
'phpdoc_types' => true,
84101
'phpdoc_var_without_name' => true,
85102
'pow_to_exponentiation' => true,
86103
'psr4' => true,
87104
'return_type_declaration' => true,
88105
'self_accessor' => true,
106+
'semicolon_after_instruction' => true,
107+
'set_type_to_cast' => true,
89108
'short_scalar_cast' => true,
109+
'simplified_null_return' => true,
90110
'single_blank_line_before_namespace' => true,
111+
'single_line_comment_style' => true,
91112
'single_quote' => true,
92113
'space_after_semicolon' => true,
114+
'standardize_increment' => true,
93115
'standardize_not_equals' => true,
94116
'strict_comparison' => true,
95117
'strict_param' => true,
96118
'ternary_operator_spaces' => true,
119+
'ternary_to_null_coalescing' => true,
97120
'trailing_comma_in_multiline_array' => true,
98121
'trim_array_spaces' => true,
99122
'whitespace_after_comma_in_array' => true,

.travis.yml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
language: php
22
sudo: false
3-
dist: trusty
43

54
php:
65
- 5.6
76
- 7.0
87
- 7.1
9-
- hhvm
8+
- 7.2
109

1110
cache:
1211
directories:
13-
- vendor
12+
- build/.composer-cache
1413

1514
before_install:
15+
- export COMPOSER_CACHE_DIR="$(pwd)/build/.composer-cache"
1616
- export XDEBUG="/home/travis/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini"
17-
- is_hhvm () { [[ $TRAVIS_PHP_VERSION =~ ^hhvm ]]; }
18-
- is_hhvm || mv -v "$XDEBUG" "$XDEBUG.disabled"
17+
- mv -v "$XDEBUG" "$XDEBUG.disabled"
1918

2019
install:
21-
- travis_retry composer update -a --no-interaction
22-
- travis_retry wget https://scrutinizer-ci.com/ocular.phar
20+
- mkdir -p build/php_codesniffer build/php-cs-fixer build/ocular
21+
- composer require --no-suggest --no-progress -n -a -d build/php-cs-fixer "friendsofphp/php-cs-fixer:^2.12"
22+
- composer require --no-suggest --no-progress -n -a -d build/php_codesniffer "squizlabs/php_codesniffer:^3.3"
23+
- composer require --no-suggest --no-progress -n -a -d build/ocular "scrutinizer/ocular:^1.5"
24+
- composer update --no-suggest --no-progress -n -a
2325

2426
script:
25-
- vendor/bin/phpcs --standard=PSR2 src tests
26-
- vendor/bin/php-cs-fixer fix -v --dry-run --allow-risky=yes --using-cache=no
27-
- if is_hhvm; then echo "xdebug.enable = On" >> /etc/hhvm/php.ini; else mv -v "$XDEBUG.disabled" "$XDEBUG"; fi
28-
- vendor/bin/phpunit --coverage-clover=coverage.clover --coverage-text
27+
- build/php_codesniffer/vendor/bin/phpcs -p --standard=PSR2 src tests
28+
- build/php-cs-fixer/vendor/bin/php-cs-fixer fix -v --dry-run --allow-risky=yes --using-cache=no
29+
- mv -v "$XDEBUG.disabled" "$XDEBUG"
30+
- vendor/bin/phpunit --coverage-clover=build/coverage.clover --coverage-text
2931

3032
after_script:
31-
- is_hhvm || php ocular.phar code-coverage:upload --format=php-clover coverage.clover
33+
- build/ocular/vendor/bin/ocular code-coverage:upload --format=php-clover build/coverage.clover

CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog #
22

3+
## v2.4.0 (2018-07-03) ##
4+
5+
* Added `string.classes` option, which allows to define an array of classes or
6+
namespaces to encode using the `::class` format, when encountered as strings
7+
* Added `string.imports` options, which allows to define the used imports to write
8+
the `::class` format strings using shorter imported notation
9+
* Support for HHVM has been dropped, as HHVM no longer aims for PHP compatibility
10+
* Added travis builds for PHP 7.2
11+
* Change some rules in the used coding standard
12+
313
## v2.3.0 (2017-07-15) ##
414

515
* Added `string.utf8` option which causes the string encoder to escape all

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2013-2017 Riikka Kalliomäki
1+
Copyright (c) 2013-2018 Riikka Kalliomäki
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of
44
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,23 @@ apply to following calls.
201201
multibyte UTF-8 characters in strings are encoded using the PHP7 unicode
202202
code point syntax. Note that this syntax does not work in PHP versions
203203
earlier than 7.0.
204+
205+
* **string.classes** : &lt;string[]&gt; ([])<br>
206+
Defines a list of classes or class namespace prefixes for classes that
207+
can be encoded using the class resolution operator `::class` when
208+
encountered in strings. e.g. providing the value `['Riimu\\']` would encode
209+
all strings that look like class names in the `Riimu` namespace like
210+
`Riimu\Kit\PHPEncoder::class`.
211+
212+
* **string.imports** : &lt;string[]&gt; ([])<br>
213+
List of imports used in the resulting code file, which allows class names
214+
to be written using shorter format. The list should be an associative array
215+
with the namespace or class as the key and the used name as the value. Use
216+
empty string to indicate the current namespace.
217+
218+
For example, if the resulting file would have `namespace Riimu\Kit\PHPEncoder;`
219+
and `use PHPUnit\Framework\TestCase;`, you could set up the array as
220+
`['Riimu\\Kit\\PHPEncoder\\' => '', 'PHPUnit\\Framework\\TestCase' => 'TestCase']`
204221

205222
* **array.short** : &lt;boolean&gt; (true)<br>
206223
When set to `true`, arrays are enclosed using square brackets `[]` instead
@@ -279,6 +296,6 @@ on HHVM.
279296

280297
## Credits ##
281298

282-
This library is Copyright (c) 2013-2017 Riikka Kalliomäki.
299+
This library is Copyright (c) 2013-2018 Riikka Kalliomäki.
283300

284301
See LICENSE for license and copying information.

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
"php": ">=5.6.0"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^5.7 || ^6.2",
26-
"squizlabs/php_codesniffer": "^3.0",
27-
"friendsofphp/php-cs-fixer": "^2.3"
25+
"phpunit/phpunit": "^7.2 || ^6.5 || ^5.7"
2826
},
2927
"suggest": {
3028
"ext-gmp": "To convert GMP numbers into PHP code"

examples/class_resolution.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/* Shows an example of how you can take advantage of class resolution in the
4+
generated code. */
5+
6+
require_once __DIR__ . '/../vendor/autoload.php';
7+
8+
$encoder = new \Riimu\Kit\PHPEncoder\PHPEncoder([
9+
'string.classes' => [
10+
'Riimu\\',
11+
'PHPUnit\\Framework\\TestCase',
12+
'DateTime',
13+
],
14+
'string.imports' => [
15+
'Riimu\\Kit\\PHPEncoder\\' => '',
16+
'PHPUnit\\Framework\\TestCase' => 'TestCase',
17+
],
18+
]);
19+
20+
21+
echo "<?php
22+
23+
namespace Riimu\Kit\PHPEncoder;
24+
25+
use PHPUnit\Framework\TestCase;
26+
27+
var_dump(";
28+
29+
echo $encoder->encode([
30+
\PHPUnit\Framework\TestCase::class,
31+
\Riimu\Kit\PHPEncoder\PHPEncoder::class,
32+
\Riimu\Kit\PHPEncoder\Encoder\Encoder::class,
33+
\DateTime::class,
34+
\DateTimeInterface::class, // Will be encoded as plain string, since it's not allowed by string.classes
35+
]);
36+
37+
echo ");\n";

src/Encoder/ArrayEncoder.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
/**
66
* Encoder for array values.
77
* @author Riikka Kalliomäki <riikka.kalliomaki@gmail.com>
8-
* @copyright Copyright (c) 2014-2017 Riikka Kalliomäki
8+
* @copyright Copyright (c) 2014-2018 Riikka Kalliomäki
99
* @license http://opensource.org/licenses/mit-license.php MIT License
1010
*/
1111
class ArrayEncoder implements Encoder
1212
{
1313
/** @var array Default values for options in the encoder */
1414
private static $defaultOptions = [
15-
'array.short' => true,
16-
'array.base' => 0,
15+
'array.short' => true,
16+
'array.base' => 0,
1717
'array.indent' => 4,
18-
'array.align' => false,
18+
'array.align' => false,
1919
'array.inline' => 70,
20-
'array.omit' => true,
21-
'array.eol' => false,
20+
'array.omit' => true,
21+
'array.eol' => false,
2222
];
2323

2424
public function getDefaultOptions()
@@ -28,7 +28,7 @@ public function getDefaultOptions()
2828

2929
public function supports($value)
3030
{
31-
return is_array($value);
31+
return \is_array($value);
3232
}
3333

3434
public function encode($value, $depth, array $options, callable $encode)
@@ -107,7 +107,7 @@ private function getInlineArray(array $lines, array $options)
107107

108108
if (preg_match('/[\r\n\t]/', $output)) {
109109
return false;
110-
} elseif ($options['array.inline'] === true || strlen($output) <= (int) $options['array.inline']) {
110+
} elseif ($options['array.inline'] === true || \strlen($output) <= (int) $options['array.inline']) {
111111
return $output;
112112
}
113113

@@ -125,7 +125,7 @@ private function buildArray(array $lines, $depth, array $options)
125125
{
126126
$indent = $this->buildIndent($options['array.base'], $options['array.indent'], $depth + 1);
127127
$last = $this->buildIndent($options['array.base'], $options['array.indent'], $depth);
128-
$eol = $options['array.eol'] === false ? PHP_EOL : (string) $options['array.eol'];
128+
$eol = $options['array.eol'] === false ? \PHP_EOL : (string) $options['array.eol'];
129129

130130
return $this->wrap(
131131
sprintf('%s%s%s,%1$s%s', $eol, $indent, implode(',' . $eol . $indent, $lines), $last),
@@ -153,10 +153,10 @@ private function wrap($string, $short)
153153
*/
154154
private function buildIndent($base, $indent, $depth)
155155
{
156-
$base = is_int($base) ? str_repeat(' ', $base) : (string) $base;
156+
$base = \is_int($base) ? str_repeat(' ', $base) : (string) $base;
157157

158158
return $depth === 0 ? $base : $base . str_repeat(
159-
is_int($indent) ? str_repeat(' ', $indent) : (string) $indent,
159+
\is_int($indent) ? str_repeat(' ', $indent) : (string) $indent,
160160
$depth
161161
);
162162
}
@@ -180,7 +180,7 @@ private function getAlignedPairs(array $array, callable $encode)
180180
$format = sprintf('%%-%ds => %%s', max(array_map('strlen', $keys)));
181181
$pairs = [];
182182

183-
for ($i = 0, $count = count($keys); $i < $count; $i++) {
183+
for ($i = 0, $count = \count($keys); $i < $count; $i++) {
184184
$pairs[] = sprintf($format, $keys[$i], $values[$i]);
185185
}
186186

@@ -225,7 +225,7 @@ private function canOmitKey($key, & $nextIndex)
225225
{
226226
$result = $key === $nextIndex;
227227

228-
if (is_int($key)) {
228+
if (\is_int($key)) {
229229
$nextIndex = max($key + 1, $nextIndex);
230230
}
231231

src/Encoder/BooleanEncoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Encoder for boolean values.
77
* @author Riikka Kalliomäki <riikka.kalliomaki@gmail.com>
8-
* @copyright Copyright (c) 2014-2017 Riikka Kalliomäki
8+
* @copyright Copyright (c) 2014-2018 Riikka Kalliomäki
99
* @license http://opensource.org/licenses/mit-license.php MIT License
1010
*/
1111
class BooleanEncoder implements Encoder
@@ -22,7 +22,7 @@ public function getDefaultOptions()
2222

2323
public function supports($value)
2424
{
25-
return is_bool($value);
25+
return \is_bool($value);
2626
}
2727

2828
public function encode($value, $depth, array $options, callable $encode)

src/Encoder/Encoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Interface for different types of value encoders.
77
* @author Riikka Kalliomäki <riikka.kalliomaki@gmail.com>
8-
* @copyright Copyright (c) 2014-2017 Riikka Kalliomäki
8+
* @copyright Copyright (c) 2014-2018 Riikka Kalliomäki
99
* @license http://opensource.org/licenses/mit-license.php MIT License
1010
*/
1111
interface Encoder

0 commit comments

Comments
 (0)