Comments (9)
Is there currently a way to add
declare(strict_types=1);
to a generated file?
No, but I'd expect a map of supported declares
to be added. Want to give it a shot?
from zend-code.
Completed untested, posting here for a sanity check of the approach:
/**
* @var array
*/
protected $declares = [];
// Add to fromReflection() ?
/**
* @return array
*/
public function getDeclares()
{
return $this->declares;
}
/**
* @param array $declares
* @return FileGenerator
*/
public function setDeclares(array $declares)
{
foreach ($declares as $directive => $value) {
$this->setDeclare($directive, $value);
}
return $this;
}
/**
* @param string $directive
* @param int|string $value
* @return FileGenerator
*/
public function setDeclare($directive, $value)
{
if (!in_array([$directive, $value], $this->declares)) {
$this->declares[] = [$directive, $value];
}
return $this;
}
public function generate() // Already exists
{
// ...
// Declares, if any
$declares = $this->getDeclares();
if ($declares) {
foreach ($declares as $directive => $value) { // Replace with array_map()?
$value = is_string($value) ? "'$value'" : $value;
$declareStatements[] = sprintf('declare(%s=%s);%s', $directive, $value, self::LINE_FEED);
}
$declareStatements = implode('', $declareStatements);
if (preg_match('#/\* Zend_Code_Generator_FileGenerator-DeclaresMarker \*/#m', $output)) {
$output = preg_replace(
'#/\* Zend_Code_Generator_FileGenerator-DeclaresMarker \*/#m',
$declareStatements,
$output,
1
);
} else {
$output .= $declareStatements;
}
}
// ...
}
from zend-code.
@GaryJones something like that, yes, although I wouldn't allow unknown declares, and wouldn't allow just strings. This enforcement can be applied via a tiny Declare
object.
I'd also simply remove setDeclare
, getDeclares
, and just allow setDeclares
from zend-code.
@Ocramius How about this:
class FileGenerator // Already exists
{
// ...
/**
* @var array
*/
protected $declares = [];
// Add to fromReflection() ?
/**
* @param Declares[] $declares
* @return FileGenerator
*/
public function setDeclares(array $declares)
{
foreach ($declares as $declare) {
if (!$declare instanceof Declare) {
throw new Exception\InvalidArgumentException(sprintf(
'%s is expecting an array of %s\Declare objects',
__METHOD__,
__NAMESPACE__
));
}
if (!in_array($declare, $this->declares) && $declare->isValid()) {
$this->declares[] = $declare;
}
}
return $this;
}
public function generate() // Already exists
{
// ...
// Declares, if any
$declares = $this->declares;
if ($declares) {
foreach ($declares as $declare) {
if ( $declare->isValid() ) {
$declareStatements[] = $declare->getStatement() . self::LINE_FEED;
}
}
$declareStatements = implode('', $declareStatements);
if (preg_match('#/\* Zend_Code_Generator_FileGenerator-DeclaresMarker \*/#m', $output)) {
$output = preg_replace(
'#/\* Zend_Code_Generator_FileGenerator-DeclaresMarker \*/#m',
$declareStatements,
$output,
1
);
} else {
$output .= $declareStatements;
}
}
// ...
}
// ...
}
class Declare
{
protected $directive = '';
protected $value;
public function __construct($directive, $value)
{
$this->directive = $directive;
$this->value = $value;
}
public function isValid() {
$allowed = [
// Directive => type
'ticks' => 'integer',
'strict_types' => 'integer',
'encoding' => 'string',
];
if (!in_array($this->directive, array_keys($allowed), true)) {
throw new InvalidArgumentException('Declare directive unknown: '.$this->directive);
}
if (gettype($this->value) !== $allowed[$this->directive]) {
throw new InvalidArgumentException('Declare value is not the expected type: '.$this->value);
}
return true;
}
public function getStatement() {
$value = is_string($this->value) ? "'$value'" : $this->value;
return 'declare('.$this->$directive.'='.$value.');';
}
}
This allows any Declare objected to be created, but only the valid ones are allowed to be queued up to be added to the file.
from zend-code.
from zend-code.
Hey, have I missed something or why doesn't this make it way to PRs? I may create one
from zend-code.
@vlakarados I never got around to it, so, please, go ahead :-)
from zend-code.
Seems like no one will do it? Then i'll do it ;)
from zend-code.
Solved in #169 which is already merged, preparing 3.4.0 release.
from zend-code.
Related Issues (20)
- Update github library description and add keywords HOT 3
- ValueGenerator array with FQCN as key with class name resolution HOT 12
- VarTag::initialize function wrong parse regex HOT 1
- 3.2.0 Release of Zend-Code introduces Backward Incompatible changes HOT 4
- Additional blank lines make generated class not PSR2 compliant HOT 10
- Add PHP 7.2 support HOT 2
- Zend\Code\Reflection\FileReflection getBody() HOT 1
- Zend\Code\Reflection\FileReflection crashes on Zend's module.config.php HOT 1
- Use 'static' return type in docblock HOT 3
- "Nesting level too deep" when comparing ClassGenerator objects HOT 1
- MethodReflection::getPrototype() crashes when method has no return HOT 5
- [BC Break] Drop built-in code scanners for 4.0 HOT 3
- Name of dynamic methods are added quotation marks HOT 1
- MethodReflection will be broken in PHP 7.3 HOT 1
- Return type self is not an internal type while code generation HOT 2
- ValueGenerator recursive setIndentation HOT 9
- Not using return value of array_map in ClassGenerator->setImplementedInterfaces HOT 1
- Reflection\MethodReflection::extractMethodContents stumbles over a static keyword HOT 1
- Create password protected rar files with zend 2 HOT 1
- Method Zend\Code\Generator\ValueGenerator::__toString() must not throw an exception (with PHP 7.3) HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zend-code.