0

For example: In sonata admin, a single admin class has always recurring syntax like

$formMapper->add('test', null, ['label' => 'testlabel']);

$formMapper->add('test1', null, ['label' => 'testlabel1']);

$formMapper->add('test2', null, ['label' => 'testlabel2']);

In this case if i am adding several fields with the add method, our quality gate is telling me, that it is a code smell using the 'label' string more than 2 times. I should add a constant for that string ...

Is it now good or bad practise to have many many classes full of constants like

const KEY_LABEL = 'label'

$formMapper->add('test2', null, [self::KEY_LABEL => 'testlabel2']);

???

I can't figure out, whats the great benefit .. if someday the label key will change, which is given by the framework, i have to make changes to this stuff either way ...

Thientvse
  • 1,753
  • 1
  • 14
  • 23
Jim Panse
  • 2,220
  • 12
  • 34
  • 1
    I don't see any reason to use it a constant. Code sniffers aren't always in the right. – Andrei Nov 10 '17 at 10:06
  • 3
    I would suggest not to change the way the bundle you're using works. As you rightly point out, labels may change and you don't want to have to go through your entire code base just to change a label. The benefit is effectively close to zero if not zero. As @Andrew says, code sniffers don't really understand the code they're checking, and in this case they are just being silly. – tchap Nov 10 '17 at 10:08
  • In addition to @tchap I think it's enough to use only PSR2. /usr/local/bin/phpcs --standard=PSR2. – staskrak Nov 10 '17 at 10:12
  • Thats a pity, that code quality evidently might suck just of such strange rules ... – Jim Panse Nov 10 '17 at 10:13

1 Answers1

0

Short answer: No. Symfony doesn't rename keys.

Long answer: Symfony has a pretty strict Backwards Compatibility Promise. It is not very likely the label key will be renamed. But that doesn't mean that it won't change.

If you have to 'rename' a key, it is very likely you have to change the value too. That means you're not renaming, but replacing it. Using a constant will help you rename the keys, but it won't help you refactoring the value.

I started working with Symfony many years ago (started with 2.1 and testing 3.4-beta right now) and I've had to perform some 'renaming' in the past. In almost all cases the key wasn't renamed but it was deprecated and later replaced by another key. One of the examples I can remember is the Choice form type refactorization, but I'm sure there are more and even better examples.

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97