Giter VIP home page Giter VIP logo

Comments (6)

ajthinking avatar ajthinking commented on June 12, 2024 1

Short answer is no.

The Default PhpParser printer "is not very suitable for refactoring code". So I extended it in PSR2PrettyPrinter and it is slightly better, but this is one of the unwanted behaviors.

from archetype.

ajthinking avatar ajthinking commented on June 12, 2024

Hi, thanks for question :)

The property method accepts either a primitive value, or an instance of a PhpParser Node instance. So we should be able to do something like this.

use PhpParser\BuilderFactory;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Scalar\String_;
  
$factory = new PhpParser\BuilderFactory();
$scriptsValueNode = $factory->concat(
    new Dir,
    new String_('/../dist/js/addon.js')
);  
  
LaravelFile::user()
  ->add()->protected()->property('scripts', $scriptsValueNode)
  ->preview()

In general I would go about it like this:

  • put an example of what you would like to write in a file
  • load it and review the AST using: LaravelFile::user()->ast()
  • see what class name your example has. In your first example that would be PhpParser\Node\Expr\ClassConstFetch
  • Use PhpParser\BuilderFactory to construct that node

from archetype.

ajthinking avatar ajthinking commented on June 12, 2024

Would love to see how you are using it when you're finished. Just a heds up, I have not looked into how PHP8 affects this package. Seems to work fine but it does not yet support constructor promotion etc. Maybe that is not to big of a problem.

from archetype.

jackmcdade avatar jackmcdade commented on June 12, 2024

Thanks for the reply! I'm back to trying to figure this out again and wondering how I might be able to pass a value like Fieldtypes\YourFieldtype:class into a class variable since it's not a string or array?

from archetype.

jackmcdade avatar jackmcdade commented on June 12, 2024

Ah, I think i just got it!

$fieldtypeClassValue = $factory->classConstFetch('Fieldtypes\KnightRider', 'class');

PHPFile::load('addons/rad-city/knight-writer/src/ServiceProvider.php')
                ->add()->protected()->property('scripts', $scriptsValue)
                ->add()->protected()->property('fieldtypes', $fieldtypeClassValue)
                ->save();

Which gave me...

class ServiceProvider extends AddonServiceProvider
{
    protected $fieldtypes = [Fieldtypes\EmptyLacroixCan::class];
    protected $scripts = [__DIR__ . '/../dist/js/addon.js'];
}

Is there any way to force some formatting? Like each value on a new line, and empty lines between classes?

from archetype.

jackmcdade avatar jackmcdade commented on June 12, 2024

Every consider tapping into something like https://github.com/FriendsOfPHP/PHP-CS-Fixer?

from archetype.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.