Skip to content

Commit

Permalink
Merge pull request #6 from Riimu/next
Browse files Browse the repository at this point in the history
Added support for class resolution operator strings
  • Loading branch information
Riimu committed Jul 3, 2018
2 parents 48e1a30 + 54d55bd commit 7e876d2
Show file tree
Hide file tree
Showing 23 changed files with 315 additions and 132 deletions.
59 changes: 41 additions & 18 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,115 @@ return \PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,

'array_syntax' => [
'syntax' => 'short'
],
'binary_operator_spaces' => [
'align_double_arrow' => null,
'align_equals' => false,
],
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'backtick_to_shell_exec' => true,
'binary_operator_spaces' => true,
'blank_line_after_opening_tag' => true,
'cast_spaces' => true,
'class_attributes_separation' => ['elements' => ['method']],
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'concat_space' => [
'spacing' => 'one'
],
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => true,
'dir_constant' => true,
'ereg_to_preg' => true,
'fully_qualified_strict_types' => true,
'function_to_constant' => true,
'function_typehint_space' => true,
'hash_to_slash_comment' => true,
'heredoc_to_nowdoc' => true,
'include' => true,
'is_null' => true,
'list_syntax' => ['syntax' => 'short'],
'logical_operators' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'magic_constant_casing' => true,
'method_separation' => true,
'modernize_types_casting' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => true,
'native_constant_invocation' => true,
'native_function_casing' => true,
'native_function_invocation' => ['include' => ['@compiler_optimized']],
'new_with_braces' => true,
'no_alias_functions' => true,
'no_binary_string' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'no_extra_blank_lines' => true,
'no_homoglyph_names' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_multiline_whitespace_before_semicolons' => true,
'no_php4_constructor' => true,
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_around_offset' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_unneeded_control_parentheses' => true,
'no_unneeded_curly_braces' => true,
'no_unneeded_final_method' => true,
'no_unreachable_default_argument_value' => true,
'no_unused_imports' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'non_printable_character' => true,
'normalize_index_brace' => true,
'object_operator_without_whitespace' => true,
'ordered_class_elements' => [
'order' => ['use_trait', 'constant', 'property', 'construct', 'method'],
],
'ordered_class_elements' => ['order' => ['use_trait', 'constant', 'property', 'construct', 'method']],
'ordered_imports' => true,
'php_unit_construct' => true,
'php_unit_dedicate_assert' => true,
'php_unit_dedicate_assert' => ['target' => 'newest'],
'php_unit_expectation' => true,
'php_unit_mock' => true,
'php_unit_namespaced' => true,
'php_unit_no_expectation_annotation' => true,
'php_unit_set_up_tear_down_visibility' => true,
'php_unit_strict' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => ['align' => 'left'],
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_inline_tag' => true,
'phpdoc_no_access' => true,
'phpdoc_no_alias_tag' => true,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_return_self_reference' => true,
'phpdoc_scalar' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types' => true,
'phpdoc_var_without_name' => true,
'pow_to_exponentiation' => true,
'psr4' => true,
'return_type_declaration' => true,
'self_accessor' => true,
'semicolon_after_instruction' => true,
'set_type_to_cast' => true,
'short_scalar_cast' => true,
'simplified_null_return' => true,
'single_blank_line_before_namespace' => true,
'single_line_comment_style' => true,
'single_quote' => true,
'space_after_semicolon' => true,
'standardize_increment' => true,
'standardize_not_equals' => true,
'strict_comparison' => true,
'strict_param' => true,
'ternary_operator_spaces' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'whitespace_after_comma_in_array' => true,
Expand Down
26 changes: 14 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
language: php
sudo: false
dist: trusty

php:
- 5.6
- 7.0
- 7.1
- hhvm
- 7.2

cache:
directories:
- vendor
- build/.composer-cache

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

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

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

after_script:
- is_hhvm || php ocular.phar code-coverage:upload --format=php-clover coverage.clover
- build/ocular/vendor/bin/ocular code-coverage:upload --format=php-clover build/coverage.clover
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog #

## v2.4.0 (2018-07-03) ##

* Added `string.classes` option, which allows to define an array of classes or
namespaces to encode using the `::class` format, when encountered as strings
* Added `string.imports` options, which allows to define the used imports to write
the `::class` format strings using shorter imported notation
* Support for HHVM has been dropped, as HHVM no longer aims for PHP compatibility
* Added travis builds for PHP 7.2
* Change some rules in the used coding standard

## v2.3.0 (2017-07-15) ##

* Added `string.utf8` option which causes the string encoder to escape all
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013-2017 Riikka Kalliomäki
Copyright (c) 2013-2018 Riikka Kalliomäki

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ apply to following calls.
multibyte UTF-8 characters in strings are encoded using the PHP7 unicode
code point syntax. Note that this syntax does not work in PHP versions
earlier than 7.0.

* **string.classes** : &lt;string[]&gt; ([])<br>
Defines a list of classes or class namespace prefixes for classes that
can be encoded using the class resolution operator `::class` when
encountered in strings. e.g. providing the value `['Riimu\\']` would encode
all strings that look like class names in the `Riimu` namespace like
`Riimu\Kit\PHPEncoder::class`.

* **string.imports** : &lt;string[]&gt; ([])<br>
List of imports used in the resulting code file, which allows class names
to be written using shorter format. The list should be an associative array
with the namespace or class as the key and the used name as the value. Use
empty string to indicate the current namespace.

For example, if the resulting file would have `namespace Riimu\Kit\PHPEncoder;`
and `use PHPUnit\Framework\TestCase;`, you could set up the array as
`['Riimu\\Kit\\PHPEncoder\\' => '', 'PHPUnit\\Framework\\TestCase' => 'TestCase']`

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

## Credits ##

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

See LICENSE for license and copying information.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
"php": ">=5.6.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.2",
"squizlabs/php_codesniffer": "^3.0",
"friendsofphp/php-cs-fixer": "^2.3"
"phpunit/phpunit": "^7.2 || ^6.5 || ^5.7"
},
"suggest": {
"ext-gmp": "To convert GMP numbers into PHP code"
Expand Down
37 changes: 37 additions & 0 deletions examples/class_resolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/* Shows an example of how you can take advantage of class resolution in the
generated code. */

require_once __DIR__ . '/../vendor/autoload.php';

$encoder = new \Riimu\Kit\PHPEncoder\PHPEncoder([
'string.classes' => [
'Riimu\\',
'PHPUnit\\Framework\\TestCase',
'DateTime',
],
'string.imports' => [
'Riimu\\Kit\\PHPEncoder\\' => '',
'PHPUnit\\Framework\\TestCase' => 'TestCase',
],
]);


echo "<?php
namespace Riimu\Kit\PHPEncoder;
use PHPUnit\Framework\TestCase;
var_dump(";

echo $encoder->encode([
\PHPUnit\Framework\TestCase::class,
\Riimu\Kit\PHPEncoder\PHPEncoder::class,
\Riimu\Kit\PHPEncoder\Encoder\Encoder::class,
\DateTime::class,
\DateTimeInterface::class, // Will be encoded as plain string, since it's not allowed by string.classes
]);

echo ");\n";
26 changes: 13 additions & 13 deletions src/Encoder/ArrayEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
/**
* Encoder for array values.
* @author Riikka Kalliomäki <[email protected]>
* @copyright Copyright (c) 2014-2017 Riikka Kalliomäki
* @copyright Copyright (c) 2014-2018 Riikka Kalliomäki
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class ArrayEncoder implements Encoder
{
/** @var array Default values for options in the encoder */
private static $defaultOptions = [
'array.short' => true,
'array.base' => 0,
'array.short' => true,
'array.base' => 0,
'array.indent' => 4,
'array.align' => false,
'array.align' => false,
'array.inline' => 70,
'array.omit' => true,
'array.eol' => false,
'array.omit' => true,
'array.eol' => false,
];

public function getDefaultOptions()
Expand All @@ -28,7 +28,7 @@ public function getDefaultOptions()

public function supports($value)
{
return is_array($value);
return \is_array($value);
}

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

if (preg_match('/[\r\n\t]/', $output)) {
return false;
} elseif ($options['array.inline'] === true || strlen($output) <= (int) $options['array.inline']) {
} elseif ($options['array.inline'] === true || \strlen($output) <= (int) $options['array.inline']) {
return $output;
}

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

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

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

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

Expand Down Expand Up @@ -225,7 +225,7 @@ private function canOmitKey($key, & $nextIndex)
{
$result = $key === $nextIndex;

if (is_int($key)) {
if (\is_int($key)) {
$nextIndex = max($key + 1, $nextIndex);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Encoder/BooleanEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Encoder for boolean values.
* @author Riikka Kalliomäki <[email protected]>
* @copyright Copyright (c) 2014-2017 Riikka Kalliomäki
* @copyright Copyright (c) 2014-2018 Riikka Kalliomäki
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class BooleanEncoder implements Encoder
Expand All @@ -22,7 +22,7 @@ public function getDefaultOptions()

public function supports($value)
{
return is_bool($value);
return \is_bool($value);
}

public function encode($value, $depth, array $options, callable $encode)
Expand Down
2 changes: 1 addition & 1 deletion src/Encoder/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Interface for different types of value encoders.
* @author Riikka Kalliomäki <[email protected]>
* @copyright Copyright (c) 2014-2017 Riikka Kalliomäki
* @copyright Copyright (c) 2014-2018 Riikka Kalliomäki
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
interface Encoder
Expand Down
Loading

0 comments on commit 7e876d2

Please sign in to comment.