2017 © Pedro PelĆ”ez
 

library spec-gen

phpspec extension for better code generation

image

memio/spec-gen

phpspec extension for better code generation

  • Tuesday, July 3, 2018
  • by gnupat
  • Repository
  • 4 Watchers
  • 59 Stars
  • 79,836 Installations
  • PHP
  • 35 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 19 Versions
  • 9 % Grown

The README.md

Memio's SpecGen Travis CI

This extension for phpspec provides a powerful code generator:, (*1)

  • method generation:
    • it inserts method at the end of the class
    • it typehints arguments (uses interface when possible)
    • it names object arguments after their type (strips Interface suffix from names)
    • it names scalar arguments after a generic name (argument)
    • it adds number on names that could collide (e.g. $argument1, $argument2)
  • constructor generation, same as method except:
    • it inserts constructor at the begining of the class
    • it inserts properties with initialization for each constructor arguments

Note: Currently it is not possible to provide custom templates to SpecGen ., (*2)

Installation

First install it using Composer:, (*3)

$ composer require --dev memio/spec-gen:^0.10

Then enable it in phpspec.yml:, (*4)

extensions:
    Memio\SpecGen\MemioSpecGenExtension: ~

Version guide:, (*5)

  • using phpspec 5? Then use spec-gen v0.9
  • using phpspec 4? Then use spec-gen v0.8
  • using phpspec 3 and PHP 7? Then use spec-gen v0.7
  • using phpspec 3 and PHP 5.6? Then use spec-gen v0.6
  • using phpspec 2? Then use spec-gen v0.4

Full example

Let's write the following specification:, (*6)

<?php

namespace spec\Vendor\Project;

use Vendor\Project\Service\Filesystem;
use Vendor\Project\File;
use PhpSpec\ObjectBehavior;

class TextEditorSpec extends ObjectBehavior
{
    const FILENAME = '/tmp/file.txt';
    const FORCE_FILE_CREATION = true;

    function let(Filesystem $filesystem)
    {
        $this->beConstructedWith($filesystem);
    }

    function it_creates_new_files(File $file, Filesystem $filesystem)
    {
        $filesystem->exists(self::FILENAME)->willReturn(false);
        $filesystem->create(self::FILENAME)->willReturn($file);

        $this->open(self::FILENAME, self::FORCE_FILE_CREATION)->shouldBe($file);
    }
}

Executing the specifications (phpspec run) will generate the following class:, (*7)

<?php

namespace Vendor\Project;

use Vendor\Project\Service\Filesystem;

class TextEditor
{
    private $filesystem;

    public function __construct(Filesytem $filesystem)
    {
        $this->filesystem = $filesystem;
    }

    public function open(string $argument1, bool $argument2)
    {
    }
}

Now we can start naming those generic arguments and write the code., (*8)

Want to know more?

You can see the current and past versions using one of the following:, (*9)

And finally some meta documentation:, (*10)

Roadmap

  • constructor property promotion
  • return type hints
  • method body (mirror of test method body)
  • better scalar argument naming (based on names used in test)

The Versions

17/09 2016

v0.6.2

0.6.2.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

04/07 2016

v0.6.1

0.6.1.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

02/07 2016

v0.6.0

0.6.0.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

13/06 2016

v0.5.0

0.5.0.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

01/12 2015

v0.4.1

0.4.1.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

01/08 2015

v0.4.0

0.4.0.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

25/05 2015

v0.3.0

0.3.0.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

15/05 2015

v0.2.0

0.2.0.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

12/05 2015

v0.1.1

0.1.1.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec

11/05 2015

v0.1.0

0.1.0.0 http://memio.github.io/spec-gen

phpspec extension for better code generation

  Sources   Download

MIT

The Requires

 

The Development Requires

extension php code generator phpspec