1. Go to this page and download the library: Download crmplease/coder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
crmplease / coder example snippets
use Crmplease\Coder\Coder;
$coder = Coder::create();
$coder->addToFileReturnArrayByOrder(
'/path/to/file.php',
// path in array
['level1', 'level2.1'],
'value'
);
[
'level1' => [
'level2' => [
// key => value or value will be added here
],
],
];
use Crmplease\Coder\Code;
new Code('$a = $b;');
use Crmplease\Coder\Constant;
new Constant('\Some\ClassName::class');
$coder->addToFileReturnArrayByOrder(
'/path/to/file.php',
// path in array
['level1.1', 'level2.1'],
// value to add
'newValue'
);
$coder->addToReturnArrayByOrder(
__DIR__ . '/path/to/ClassName.php',
// method name
'getArray',
// path in array
['level1.1', 'level2.1'],
// value to add
'newValue'
);
// file /path/to/ClassName.php
class ClassName
{
public function getArray(): array
{
return [
'level1.1' => [
'level2.1' => [
'existsValue',
],
],
'level1.2' => [
'value',
],
];
}
}
$coder->addToPropertyArrayByOrder(
__DIR__ . '/path/to/ClassName.php',
// property name
'array',
// path in array
['level1.1', 'level2.1'],
// value to add
'newValue'
);
$coder->addToFileReturnArrayByKey(
'/path/to/file.php',
// path in array
['level1.1', 'level2.1'],
// key to add
'newKey',
// value to add
'newValue'
);
$coder->addToReturnArrayByKey(
__DIR__ . '/path/to/ClassName.php',
// method name
'getArray',
// path in array
['level1.1', 'level2.1'],
// key to add
'newKey',
// value to add
'newValue'
);
$coder->addToPropertyArrayByKey(
__DIR__ . '/path/to/ClassName.php',
// property name
'array',
// path in array
['level1.1', 'level2.1'],
// key to add
'newKey',
// value to add
'newValue'
);
use Crmplease\Coder\Rector\AddPropertyToClassRector;
$coder->addPropertyToClass(
'/path/to/ClassName.php',
// property name
'newProperty',
// pass true if property should be static
false,
// property visibility, can be VISIBILITY_PRIVATE, VISIBILITY_PROTECTED or VISIBILITY_PUBLIC
AddPropertyToClassRector::VISIBILITY_PRIVATE,
// default value for property, skip it or pass null if isn't needed
'newValue',
// property type in Phpdoc
'string',
// property description in Phpdoc
'description'
);
// file /path/to/ClassName.php
class ClassName
{
protected $existsProperty;
}
$coder->addParameterToMethod(
'/path/to/ClassName.php',
// method name
'__construct',
// parameter name
'newParameter',
// parameter type
'?string',
// if parameter has default value, then true
true,
// parameter default value
'newValue'
);
// file /path/to/ClassName.php
class ClassName
{
public function __construct(int $existsParameter = 123) {}
}
// file /path/to/ClassName.php
class ClassName
{
public function __construct(int $existsParameter = 123, ?string $newParameter = 'newValue') {}
}
$coder->addCodeToMethod(
'/path/to/ClassName.php',
// method name
'__construct',
// code as string
'$this->newProperty = $newParameter;'
);
// file /path/to/ClassName.php
class ClassName
{
protected $existsProperty;
private $newProperty;
public function __construct(int $existsParameter = 123, ?string $newParameter = 'newValue')
{
$this->existsProperty = $existsParameter;
}
}
// file /path/to/ClassName.php
class ClassName
{
protected $existsProperty;
private $newProperty;
public function __construct(int $existsParameter = 123, ?string $newParameter = 'newValue')
{
$this->existsProperty = $existsParameter;
$this->newProperty = $newParameter;
}
}
use Crmplease\Coder\Rector\AddMethodToClassRector;
$coder->addMethodToClass(
'/path/to/ClassName.php',
// method name
'newMethod',
// method visibility, can be VISIBILITY_PRIVATE, VISIBILITY_PROTECTED or VISIBILITY_PUBLIC
AddMethodToClassRector::VISIBILITY_PUBLIC,
// pass true if method should be static
false,
// pass true if method should be abstract
false,
// pass true if method should be final
false,
// method return type
'int',
// return description
'return description',
// method description
'method description'
);
// file /path/to/ClassName.php
class ClassName
{
/**
* Exists description
*
* @return int exists return description
*/
public function existsMethod(): int
{
return 0;
}
}
// file /path/to/ClassName.php
class ClassName
{
/**
* Exists description
*
* @return int exists return description
*/
public function existsMethod(): int
{
return 0;
}
/**
* method description
* @return int return description
*/
public function newMethod(): int
{
return 0;
}
}
$coder->addPhpdocParamToMethod(
'/path/to/ClassName.php',
// method name
'__construct',
// parameter, for which need to add Phpdoc
'newParameter',
// parameter type
'string|null',
// description for parameter
'some description'
);
// file /path/to/ClassName.php
class ClassName
{
/**
* @param int $existsParameter
*/
public function __construct(int $existsParameter = 123, ?string $newParameter = 'newValue') {}
}
// file /path/to/ClassName.php
class ClassName
{
/**
* @param int $existsParameter
* @param string|null $newParameter some description
*/
public function __construct(int $existsParameter = 123, ?string $newParameter = 'newValue') {}
}
use \Crmplease\Coder\PhpdocProperty;
$coder->addPhpdocPropertyToClass(
'/path/to/ClassName.php',
new PhpdocProperty(
// property name
'newProperty',
// property type, default is mixed
'string|null',
// description for property
'some description'
)
);
// file /path/to/ClassName.php
/**
* @property int $existsProperty
*/
class ClassName {}
// file /path/to/ClassName.php
/**
* @property int $existsProperty
* @property string|null $newProperty some description
*/
class ClassName {}
use \Crmplease\Coder\PhpdocProperty;
$coder->addPhpdocPropertiesToClass(
'/path/to/ClassName.php',
[
new PhpdocProperty('newProperty1'),
new PhpdocProperty('newProperty2'),
]
);
use \Crmplease\Coder\PhpdocMethod;
use \Crmplease\Coder\PhpdocMethodParameter;
$coder->addPhpdocMethodToClass(
'/path/to/ClassName.php',
new PhpdocMethod(
// method name
'newMethod',
// return type, default is mixed
'string|null',
// true if should be static
false,
// array of parameters
[
new PhpdocMethodParameter(
// parameter name
'parameter1',
// parameter type
'int',
// has default value if true
true,
// default value
0
),
],
// description for method
'some description'
)
);
// file /path/to/ClassName.php
/**
* @method int existsMethod()
*/
class ClassName {}
// file /path/to/ClassName.php
/**
* @method int existsMethod()
* @method string|null newMethod(int $parameter1 = 0) some description
*/
class ClassName {}
use \Crmplease\Coder\PhpdocMethod;
$coder->addPhpdocMethodsToClass(
'/path/to/ClassName.php',
[
new PhpdocMethod('newMethod1'),
new PhpdocMethod('newMethod2'),
]
);
// file /path/to/ClassName.php
class ClassName extends \Some\ParentClass {}
// file /path/to/ClassName.php
class ClassName extends \Some\OtherClass {}
use Crmplease\Coder\Code;
use Crmplease\Coder\Constant;
$coder->addToFileReturnArrayByOrder(
'/path/to/file.php',
// path in array
['level1.1', new Constant('\Some\ClassName::class')],
// value to add
[
'newValue',
new Constant('\Some\ClassName::SOME_CONSTANT'),
new Code('Rule::unique(\'countries\')->ignore($country->getKey())'),
]
);
use Crmplease\Coder\Coder;
use Crmplease\Coder\Config;
$config = new Config();
$coder = Coder::create($config);
use Crmplease\Coder\Config;
$config = (new Config())
->setShowProgressBar(false);
use Crmplease\Coder\Config;
$config = (new Config())
// use default value
->setAutoImport(null)
// always auto import
->setAutoImport(true)
// newer auto import
->setAutoImport(false)
->setAutoImport(
[
// auto import this file
'/path/to/file/with/enabled/auto/import/classes.php' => true,
// doesn't auto import this file
'/path/to/file/with/disabled/auto/import/classes.php' => false,
// use default value
'/path/to/file/with/defaul/auto/import/classes.php' => null,
]
)
->setAutoImport(
static function (string $file): ?bool {
// some logic
// if null is returned, then default value will be used
return $result;
}
)
->setAutoImport([SomeClass::class, 'method']);
use Crmplease\Coder\Config;
$config = (new Config())
->setRectorConfigPath('/path/to/rector.php');