generate() public static method

Creates a class definition.
public static generate ( array $options = [] ) : string
$options array Array of options. Options are: - `'class'` _string_ : the fully-namespaced class name. - `'extends'` _string_ : the fully-namespaced parent class name. - `'implements'` _array_ : the implemented interfaces. - `'uses'` _array_ : the used traits. - `'methods'` _array_ : the methods to stubs. - `'layer'` _boolean_: indicate if public methods should be layered.
return string The generated class string content.
Beispiel #1
0
 /**
  * Helper for `Layer::process()`.
  *
  * @param array $parent The node instance tor process.
  */
 protected function _processTree($parent)
 {
     foreach ($parent->tree as $node) {
         if ($node->processable && $node->type === 'class' && $node->extends) {
             $namespace = $node->namespace->name . '\\';
             $parent = $node->extends;
             $extends = ltrim($parent[0] === '\\' ? $parent : $namespace . $parent, '\\');
             if (!isset($this->_override[$extends])) {
                 continue;
             }
             $layerClass = $node->name . $this->_suffix;
             $node->extends = $layerClass;
             $pattern = preg_quote($parent);
             $node->body = preg_replace("~(extends\\s+){$pattern}~", "\\1{$layerClass}", $node->body);
             $code = Double::generate(['class' => $layerClass, 'extends' => $extends, 'openTag' => false, 'closeTag' => false, 'layer' => true]);
             $parser = $this->_classes['parser'];
             $root = $parser::parse($code, ['php' => true]);
             $node->close .= str_replace("\n", '', $parser::unparse($this->_pointcut->process($root)));
         } elseif (count($node->tree)) {
             $this->_processTree($node);
         }
     }
 }
Beispiel #2
0
            expect($double->getTimestamp())->toBe(12345678);
        });
        it("adds ` = NULL` to optional parameter in PHP core method", function () {
            skipIf(defined('HHVM_VERSION'));
            $result = Double::generate(['class' => 'Kahlan\\Spec\\Plugin\\Double\\Double', 'extends' => 'LogicException', 'layer' => true]);
            $expected = <<<EOD
<?php
namespace Kahlan\\\\Spec\\\\Plugin\\\\Double;

class Double extends \\\\LogicException {

    public function __construct\\(\\\$message = NULL, \\\$code = NULL, \\\$previous = NULL\\)
EOD;
            expect($result)->toMatch('~' . $expected . '~i');
        });
        it("generates code without PHP tags", function () {
            $result = Double::generate(['class' => 'Kahlan\\Spec\\Plugin\\Double\\Double', 'magicMethods' => false, 'openTag' => false, 'closeTag' => false]);
            $expected = <<<EOD
namespace Kahlan\\Spec\\Plugin\\Double;

class Double {



}

EOD;
            expect($result)->toBe($expected);
        });
    });
});