Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 161 lines (143 sloc) 6.478 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?php

namespace FOQ\TyperBundle;

use Zend\CodeGenerator\Php\PhpFile;
use Zend\CodeGenerator\Php\PhpClass;
use Zend\CodeGenerator\Php\PhpProperty;
use Zend\CodeGenerator\Php\PhpDocblock;
use Zend\CodeGenerator\Php\PhpMethod;
use Zend\CodeGenerator\Php\PhpParameter;

/**
* Writes the class code
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
class Typer
{
    protected $classDefinition;

    public function __construct(ClassDefinition $classDefinition)
    {
        $this->classDefinition = $classDefinition;
    }

    public function write()
    {
        $class = new PhpClass();
        $class->setName($this->classDefinition->getName());
        if ($extends = $this->classDefinition->getExtends()) {
            $class->setExtendedClass($this->classDefinition->extractClassName($extends));
        }
        $implements = $extends = $this->classDefinition->getImplements();
        $implementedInterfaces = array();
        foreach ($this->classDefinition->getImplements() as $implement) {
            $implementedInterfaces[] = $this->classDefinition->extractClassName($implement);
        }
        $class->setImplementedInterfaces($implementedInterfaces);

        if ($comment = $this->classDefinition->getComment()) {
            $class->setDocblock(new PhpDocblock(array(
                'shortDescription' => $this->classDefinition->getComment()
            )));
        }

        $constructorProperties = $this->classDefinition->getPropertiesInjectedInConstructor();
        if (!empty($constructorProperties)) {
            $method = new PhpMethod();
            $method->setName('__construct');
            foreach ($constructorProperties as $propertyDefinition) {
                $parameter = new PhpParameter();
                $parameter->setName($propertyDefinition->getName());
                if (!$this->isNativeType($propertyDefinition->getType())) {
                    $parameter->setType($propertyDefinition->getType());
                }
                $method->setParameter($parameter);
            }
            $body = array();
            foreach ($constructorProperties as $propertyDefinition) {
                $body[] = sprintf('$this->%s = $%s;', $propertyDefinition->getName(), $propertyDefinition->getName());
            }
            $method->setBody(implode("\n", $body));
            $docBlock = new PhpDocblock();
            if ($comment = $propertyDefinition->getComment()) {
                $docBlock->setShortDescription('Instanciates a new '.$this->classDefinition->getClassName());
            }
            foreach ($constructorProperties as $propertyDefinition) {
                $docBlock->setTag(array(
                    'name' => '@param',
                    'description' => $propertyDefinition->getType().' '.$propertyDefinition->getName()
                ));
            }
            $method->setDocblock($docBlock);
            $class->setMethod($method);
        }

        foreach ($this->classDefinition->getProperties() as $propertyDefinition) {
            $property = new PhpProperty();
            $property->setName($propertyDefinition->getName());
            $property->setVisibility($propertyDefinition->getVisibility());
            $property->setDefaultValue($propertyDefinition->getDefault());
            $docBlock = new PhpDocblock();
            if ($comment = $propertyDefinition->getComment()) {
                $docBlock->setShortDescription($comment);
            }
            $docBlock->setTag(array(
                'name' => '@var',
                'description' => $propertyDefinition->getType()
            ));
            $property->setDocblock($docBlock);

            $class->setProperty($property);

            if ($propertyDefinition->hasGetter()) {
                $method = new PhpMethod();
                $method->setName('get'.ucfirst($propertyDefinition->getName()));
                $method->setBody(sprintf('return $this->%s;', $propertyDefinition->getName()));
                $docBlock = new PhpDocblock();
                if ($comment = $propertyDefinition->getComment()) {
                    $docBlock->setShortDescription('Gets: '.$comment);
                }
                $docBlock->setTag(array(
                    'name' => '@return',
                    'description' => $propertyDefinition->getType().' '.$propertyDefinition->getName()
                ));
                $method->setDocblock($docBlock);
                $class->setMethod($method);
            }

            if ($propertyDefinition->hasSetter()) {
                $method = new PhpMethod();
                $method->setName('set'.ucfirst($propertyDefinition->getName()));
                $parameter = new PhpParameter();
                $parameter->setName($propertyDefinition->getName());
                if (!$this->isNativeType($propertyDefinition->getType())) {
                    $parameter->setType($propertyDefinition->getType());
                }
                $method->setParameter($parameter);
                $method->setBody(sprintf('$this->%s = $%s;', $propertyDefinition->getName(), $propertyDefinition->getName()));
                $docBlock = new PhpDocblock();
                if ($comment = $propertyDefinition->getComment()) {
                    $docBlock->setShortDescription('Sets: '.$comment);
                }
                $docBlock->setTag(array(
                    'name' => '@param',
                    'description' => $propertyDefinition->getType().' '.$propertyDefinition->getName()
                ));
                $method->setDocblock($docBlock);
                $class->setMethod($method);
            }
        }

        $code = $class->generate();
        $locs = explode("\n", $code);

        // remove header
        $code = substr($code, strpos($code, "\n") +1);
        $code = substr($code, strpos($code, "\n") +1);

        // add header
        $headerWriter = new HeaderTyper($this->classDefinition->getNamespace(), $this->classDefinition->getUses());
        $code = $headerWriter->write()."\n".$code;

        // trim lines
        $codeTrimmer = new CodeTrimmer($code);
        $code = $codeTrimmer->trim($code);

        return $code;
    }

    /**
* Tells whether a given type is a native PHP type or not
*
* @return null
**/
    public function isNativeType($type)
    {
        return in_array(strtolower($type), array('int', 'string', 'bool', 'boolean', 'array'));
    }
}
Something went wrong with that request. Please try again.