Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #170 from webimpress/feature/constant-visibility
Browse files Browse the repository at this point in the history
Feature: constant visibility
  • Loading branch information
Ocramius committed Oct 5, 2019
2 parents ebd7eb9 + 7a2f0dd commit a589c72
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/Generator/PropertyGenerator.php
Expand Up @@ -157,7 +157,6 @@ public function __construct($name = null, $defaultValue = null, $flags = self::F
public function setConst($const)
{
if ($const) {
$this->removeFlag(self::FLAG_PUBLIC | self::FLAG_PRIVATE | self::FLAG_PROTECTED);
$this->setFlags(self::FLAG_CONSTANT);
} else {
$this->removeFlag(self::FLAG_CONSTANT);
Expand Down Expand Up @@ -227,7 +226,7 @@ public function generate()
$this->name
));
}
$output .= $this->indentation . 'const ' . $name . ' = '
$output .= $this->indentation . $this->getVisibility() . ' const ' . $name . ' = '
. ($defaultValue !== null ? $defaultValue->generate() : 'null;');

return $output;
Expand Down
2 changes: 1 addition & 1 deletion test/Generator/ClassGeneratorTest.php
Expand Up @@ -802,7 +802,7 @@ public function testClassCanBeGeneratedWithConstantAndPropertyWithSameName()
class TestSampleSingleClass
{
const fooProperty = 'duplicate';
public const fooProperty = 'duplicate';
public $fooProperty = true;
Expand Down
4 changes: 2 additions & 2 deletions test/Generator/InterfaceGeneratorTest.php
Expand Up @@ -152,9 +152,9 @@ public function testCodeGenerationShouldTakeIntoAccountNamespacesFromReflection(
interface FooInterface
{
const BAR = 5;
public const BAR = 5;
const FOO = 5;
public const FOO = 5;
public function fooBarBaz();
Expand Down
21 changes: 19 additions & 2 deletions test/Generator/PropertyGeneratorTest.php
Expand Up @@ -9,6 +9,7 @@

namespace ZendTest\Code\Generator;

use Generator;
use PHPUnit\Framework\TestCase;
use Zend\Code\Generator\DocBlock\Tag\VarTag;
use Zend\Code\Generator\DocBlockGenerator;
Expand Down Expand Up @@ -126,10 +127,26 @@ public function testPropertyMultilineValue() : void
self::assertEquals($expectedSource, $targetSource);
}

public function visibility() : Generator
{
yield 'public' => [PropertyGenerator::FLAG_PUBLIC, 'public'];
yield 'protected' => [PropertyGenerator::FLAG_PROTECTED, 'protected'];
yield 'private' => [PropertyGenerator::FLAG_PRIVATE, 'private'];
}

/**
* @dataProvider visibility
*/
public function testPropertyCanProduceConstatWithVisibility(int $flag, string $visibility) : void
{
$codeGenProperty = new PropertyGenerator('FOO', 'bar', [PropertyGenerator::FLAG_CONSTANT, $flag]);
self::assertSame(' ' . $visibility . ' const FOO = \'bar\';', $codeGenProperty->generate());
}

public function testPropertyCanProduceContstantModifier() : void
{
$codeGenProperty = new PropertyGenerator('someVal', 'some string value', PropertyGenerator::FLAG_CONSTANT);
self::assertEquals(' const someVal = \'some string value\';', $codeGenProperty->generate());
self::assertEquals(' public const someVal = \'some string value\';', $codeGenProperty->generate());
}

/**
Expand All @@ -139,7 +156,7 @@ public function testPropertyCanProduceContstantModifierWithSetter() : void
{
$codeGenProperty = new PropertyGenerator('someVal', 'some string value');
$codeGenProperty->setConst(true);
self::assertEquals(' const someVal = \'some string value\';', $codeGenProperty->generate());
self::assertEquals(' public const someVal = \'some string value\';', $codeGenProperty->generate());
}

public function testPropertyCanProduceStaticModifier() : void
Expand Down
27 changes: 15 additions & 12 deletions test/Generator/ValueGeneratorTest.php
Expand Up @@ -92,33 +92,36 @@ public function validConstantTypes()
return [
[
new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY, ValueGenerator::OUTPUT_SINGLE_LINE),
' const FOO = [];',
' public const FOO = [];',
],
[
new PropertyValueGenerator(
[],
PropertyValueGenerator::TYPE_ARRAY_LONG,
ValueGenerator::OUTPUT_SINGLE_LINE
),
' const FOO = array();',
' public const FOO = array();',
],
[
new PropertyValueGenerator(
[],
PropertyValueGenerator::TYPE_ARRAY_SHORT,
ValueGenerator::OUTPUT_SINGLE_LINE
),
' const FOO = [];',
' public const FOO = [];',
],
[new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOL), ' public const FOO = true;'],
[new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOLEAN), ' public const FOO = true;'],
[new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INT), ' public const FOO = 1;'],
[new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INTEGER), ' public const FOO = 1;'],
[new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_DOUBLE), ' public const FOO = 0.1;'],
[new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_FLOAT), ' public const FOO = 0.1;'],
[new PropertyValueGenerator('bar', PropertyValueGenerator::TYPE_STRING), " public const FOO = 'bar';"],
[new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL), ' public const FOO = null;'],
[
new PropertyValueGenerator('PHP_EOL', PropertyValueGenerator::TYPE_CONSTANT),
' public const FOO = PHP_EOL;',
],
[new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOL), ' const FOO = true;'],
[new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOLEAN), ' const FOO = true;'],
[new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INT), ' const FOO = 1;'],
[new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INTEGER), ' const FOO = 1;'],
[new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_DOUBLE), ' const FOO = 0.1;'],
[new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_FLOAT), ' const FOO = 0.1;'],
[new PropertyValueGenerator('bar', PropertyValueGenerator::TYPE_STRING), " const FOO = 'bar';"],
[new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL), ' const FOO = null;'],
[new PropertyValueGenerator('PHP_EOL', PropertyValueGenerator::TYPE_CONSTANT), ' const FOO = PHP_EOL;'],
];
}

Expand Down
4 changes: 2 additions & 2 deletions test/TestAsset/BarClass.php
Expand Up @@ -9,8 +9,8 @@

abstract class BarClass
{
const BAR = 5;
const FOO = self::BAR;
public const BAR = 5;
public const FOO = self::BAR;

protected static $bar = 'value';

Expand Down
4 changes: 2 additions & 2 deletions test/TestAsset/FooClass.php
Expand Up @@ -10,8 +10,8 @@

abstract class FooClass implements \ArrayAccess, E\Blarg, Local\SubClass
{
const BAR = 5;
const FOO = self::BAR;
public const BAR = 5;
public const FOO = self::BAR;

/**
* Constant comment
Expand Down
4 changes: 2 additions & 2 deletions test/TestAsset/FooInterface.php
Expand Up @@ -11,8 +11,8 @@

interface FooInterface extends \ArrayAccess
{
const BAR = 5;
const FOO = self::BAR;
public const BAR = 5;
public const FOO = self::BAR;

public function fooBarBaz();

Expand Down

0 comments on commit a589c72

Please sign in to comment.