Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/contorion feature set #1

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6ec60f5
added interface generator and tests
Jul 23, 2015
5021825
reverted change
Jul 23, 2015
8c0af21
changed array syntax to help compatibility
Jul 23, 2015
13aafe6
added TraitBlock and require php5.4
Jul 23, 2015
af62967
fixed formatting
Jul 23, 2015
249fbb4
Merge branch 'feature/generate-traits' into feature/contorion-feature…
Jul 23, 2015
a0955ea
added cs fixer
Jul 23, 2015
09797a4
cs fixed
Jul 23, 2015
d4f412e
cs fix for tests
Jul 23, 2015
fba94d5
formatting
Jul 23, 2015
4ca412b
cs changes to the generators
Jul 23, 2015
c75e0d7
fixed test bugs resulting from naming problems in namespaces
Jul 23, 2015
4d6a356
complied name to cs fix
Jul 23, 2015
b6a737b
Release 0.5.0
Jul 23, 2015
51b2d79
Release 0.5.0
Jul 23, 2015
56c73ef
added static properties
Jul 23, 2015
116f4fe
Merge branch 'feature/static-properties' into feature/contorion-featu…
Jul 23, 2015
0394fca
- changed dumo behavior
Oct 21, 2015
a247240
- refactoring and new doc block handling
Oct 22, 2015
62e9fa6
Release 0.6.1
Oct 22, 2015
d497a64
Release 0.6.2
Oct 22, 2015
0835e95
Release 0.6.3
Oct 22, 2015
fbfa666
- added import uses to classes
Oct 22, 2015
27eff96
- added to interface as well
Oct 22, 2015
47e74a0
- fixed doc block placement
Oct 22, 2015
3ef73ba
- formatting
Oct 22, 2015
b8d0bd4
- fromatting
Oct 22, 2015
ae44c1f
fixed class doc block
Oct 22, 2015
f91b38b
-added getter for parameter default value
Oct 22, 2015
c3da7f8
- added getter for optional
Oct 22, 2015
f2490c5
- added wrapper around array keys
Dec 17, 2015
6501bd8
BOSS-30433 Update variable in string to php8
j-lukowicz-contorion Nov 14, 2023
83de321
Merge pull request #2 from contorion/ticket/BOSS-30433-update-quentin…
j-lukowicz-contorion Nov 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

$finder = Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__ . '/src')
->exclude('Generated')
;
return Symfony\CS\Config\Config::create()
->fixers(
array(
'controls-spaces',
'braces',
'elseif',
'eof_ending',
'extra_empty_lines',
'function_declaration',
'include',
'indentation',
'linefeed',
'php_closing_tag',
'psr0',
'short_tag',
'trailing_spaces',
'unused_use',
'visibility'
)
)
->finder($finder)
;
34 changes: 19 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"name": "tomaszdurka/codegenerator",
"authors": [
{
"name": "Tomasz Durka",
"email": "[email protected]"
"name": "tomaszdurka/codegenerator",
"authors": [
{
"name": "Tomasz Durka",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4"
},
"require-dev": {
"fabpot/php-cs-fixer": "^1.9",
"phpunit/phpunit": "~3.7.10",
"satooshi/php-coveralls": "~0.6.1"
},
"autoload": {
"psr-0": {
"CodeGenerator\\": "source/"
}
}
],
"require-dev": {
"phpunit/phpunit": "~3.7.10",
"satooshi/php-coveralls": "~0.6.1"
},
"autoload": {
"psr-0": {
"CodeGenerator\\": "source/"
}
}
}
36 changes: 20 additions & 16 deletions source/CodeGenerator/ArrayBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,51 @@

namespace CodeGenerator;

class ArrayBlock extends Block {

class ArrayBlock extends Block
{
/** @var array */
private $_value;
private $value;

/**
* @param array $value
*/
public function __construct(array $value = null) {
$this->_value = (array) $value;
public function __construct(array $value = null)
{
$this->value = (array)$value;
}

/**
* @return string
*/
public function dump() {
$entries = array();
protected function dumpContent()
{
$entries = [];
$isAssociative = $this->isAssociative();
foreach ($this->_value as $key => $value) {
foreach ($this->value as $key => $value) {
$line = '';
if ($isAssociative) {
$line .= $key . ' => ';
$line .= '\'' . $key . '\' => ';
}
$value = new ValueBlock($value);
$line .= $value->dump();
$entries[] = $line;
}
$content = implode(', ', $entries);
if (strlen($content) < 100) {
return 'array(' . $content . ')';
return '[' . $content . ']';
} else {
$content = implode(",\n", $entries);
return $this->_dumpLine(
'array(',
$this->_indent($content),
')'

return $this->dumpLine(
'[',
$this->indent($content),
']'
);
}
}

public function isAssociative() {
return (bool) count(array_filter(array_keys($this->_value), 'is_string'));
public function isAssociative()
{
return (bool)count(array_filter(array_keys($this->value), 'is_string'));
}
}
150 changes: 111 additions & 39 deletions source/CodeGenerator/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,166 @@

namespace CodeGenerator;

abstract class Block {

abstract class Block implements GeneratorConstants
{
/** @var string */
protected static $_indentation = ' ';
protected static $indentation = ' ';
/** @var DocBlock */
protected $docBlock;

/**
* @param string $indentation
*/
public static function setIndentation($indentation)
{
self::$indentation = (string)$indentation;
}

/**
* @param string $className
*
* @return string
*/
abstract public function dump();
protected static function normalizeClassName($className)
{
if (strpos($className, '\\') !== 0) {
$className = '\\' . $className;
}

return $className;
}

public function __toString() {
/**
* @param DocBlock $docBlock
*
* @return $this
*/
public function setDocBlock($docBlock)
{
$this->docBlock = $docBlock;

return $this;
}

/**
* @return string
*/
public function __toString()
{
return $this->dump();
}

/**
* @return string
*/
public function dump()
{
if ($this->docBlock) {
$docBlockText = $this->docBlock->dump();
if ($docBlockText) {
$docBlockText .= PHP_EOL;
}

return $docBlockText . '' . $this->dumpContent();
}

return $this->dumpContent();
}

/**
* @return string
*/
abstract protected function dumpContent();

/**
* @param string $content
*
* @return string
*/
protected function _indent($content) {
return preg_replace('/(:?^|[\n])/', '$1' . self::$_indentation, $content);
protected function indent($content)
{
return preg_replace('/(:?^|[\n])/', '$1' . self::$indentation, $content);
}

/**
* @param string $content
* @param boolean|null $untilUnsafe
* @param string $content
* @param bool|null $untilUnsafe
*
* @return string
*/
protected function _outdent($content, $untilUnsafe = null) {
$indentation = self::$_indentation;
protected function outdent($content, $untilUnsafe = null)
{
$indentation = self::$indentation;
if (!$indentation) {
return $content;
}
$lines = explode(PHP_EOL, $content);
if ($untilUnsafe) {
$nonemptyLines = array_filter($lines, function ($line) {
return (bool) trim($line);
});
$unsafeLines = array_filter($nonemptyLines, function ($line) use ($indentation) {
return strpos($line, $indentation) !== 0;
});
$nonemptyLines = array_filter(
$lines,
function ($line) {
return (bool)trim($line);
}
);
$unsafeLines = array_filter(
$nonemptyLines,
function ($line) use ($indentation) {
return strpos($line, $indentation) !== 0;
}
);
if (count($unsafeLines) || !count($nonemptyLines)) {
return $content;
}
}
foreach ($lines as $key => $line) {
$lines[$key] = preg_replace('/^' . preg_quote(self::$_indentation) . '/', '$1', $line);
$lines[$key] = preg_replace('/^' . preg_quote(self::$indentation) . '/', '$1', $line);
}
$content = implode(PHP_EOL, $lines);
if ($untilUnsafe) {
$content = $this->_outdent($content, $untilUnsafe);
$content = $this->outdent($content, $untilUnsafe);
}

return $content;
}

/**
* @param string $line , $line, $line
*
* @return string
*/
protected function _dumpLine($line) {
protected function dumpLine($line)
{
$lines = func_get_args();
return $this->_dumpLines($lines);
}

/**
* @param string[] $lines
* @return string
*/
protected function _dumpLines(array $lines) {
return implode(PHP_EOL, array_filter($lines, function ($element) {
return !is_null($element);
}));
return $this->dumpLines($lines);
}

/**
* @param string $indentation
* @param \Reflector $reflection
*/
public static function setIndentation($indentation) {
self::$_indentation = (string) $indentation;
protected function setDocBlockFromReflection(\Reflector $reflection)
{
$block = DocBlock::createMethodDocBlockFromReflection($reflection);
if ($block) {
$this->setDocBlock($block);
}
}

/**
* @param string $className
* @param string[] $lines
*
* @return string
*/
protected static function _normalizeClassName($className) {
if (strpos($className, '\\') !== 0) {
$className = '\\' . $className;
}
return $className;
protected function dumpLines(array $lines)
{
return implode(
PHP_EOL,
array_filter(
$lines,
function ($element) {
return !is_null($element);
}
)
);
}
}
Loading