Skip to content

Commit

Permalink
Make UnderscoreNamingStrategy number-aware only
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed May 8, 2022
1 parent c2d1686 commit 6034473
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 131 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Upgrade to 3.0

## BC BREAK: `UnderscoreNamingStrategy` is number aware only

The second argument to `UnderscoreNamingStrategy::__construct()` was dropped,
the strategy can no longer be unaware of numbers.

## BC BREAK: Remove `Doctrine\ORM\Proxy\Autoloader`

Use `Doctrine\Common\Proxy\Autoloader` instead.
Expand Down
24 changes: 3 additions & 21 deletions lib/Doctrine/ORM/Mapping/UnderscoreNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Doctrine\ORM\Mapping;

use Doctrine\Deprecations\Deprecation;

use function preg_replace;
use function str_contains;
use function strrpos;
Expand All @@ -24,33 +22,17 @@
*/
class UnderscoreNamingStrategy implements NamingStrategy
{
private const DEFAULT_PATTERN = '/(?<=[a-z])([A-Z])/';
private const NUMBER_AWARE_PATTERN = '/(?<=[a-z0-9])([A-Z])/';

/** @var int */
private $case;

/** @var string */
private $pattern;

/**
* Underscore naming strategy construct.
*
* @param int $case CASE_LOWER | CASE_UPPER
*/
public function __construct($case = CASE_LOWER, bool $numberAware = false)
public function __construct($case = CASE_LOWER)
{
if (! $numberAware) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/7908',
'Creating %s without setting second argument $numberAware=true is deprecated and will be removed in Doctrine ORM 3.0.',
self::class
);
}

$this->case = $case;
$this->pattern = $numberAware ? self::NUMBER_AWARE_PATTERN : self::DEFAULT_PATTERN;
$this->case = $case;
}

/**
Expand Down Expand Up @@ -137,7 +119,7 @@ public function joinKeyColumnName($entityName, $referencedColumnName = null)

private function underscore(string $string): string
{
$string = preg_replace($this->pattern, '_$1', $string);
$string = preg_replace('/(?<=[a-z0-9])([A-Z])/', '_$1', $string);

if ($this->case === CASE_UPPER) {
return strtoupper($string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public function testNamingStrategy(): void

self::assertEquals('ID', $class->getColumnName('id'));
self::assertEquals('NAME', $class->getColumnName('name'));
self::assertEquals('DDC1476ENTITY_WITH_DEFAULT_FIELD_TYPE', $class->table['name']);
self::assertEquals('DDC1476_ENTITY_WITH_DEFAULT_FIELD_TYPE', $class->table['name']);
}

/**
Expand Down
90 changes: 16 additions & 74 deletions tests/Doctrine/Tests/ORM/Mapping/NamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ private static function underscoreNamingUpper(): UnderscoreNamingStrategy
return new UnderscoreNamingStrategy(CASE_UPPER);
}

private static function numberAwareUnderscoreNamingLower(): UnderscoreNamingStrategy
{
return new UnderscoreNamingStrategy(CASE_LOWER, true);
}

private static function numberAwareUnderscoreNamingUpper(): UnderscoreNamingStrategy
{
return new UnderscoreNamingStrategy(CASE_UPPER, true);
}

/**
* Data Provider for NamingStrategy#classToTableName
*
Expand All @@ -60,20 +50,12 @@ public static function dataClassToTableName(): array
// UnderscoreNamingStrategy
[self::underscoreNamingLower(), 'some_class_name', '\Name\Space\SomeClassName'],
[self::underscoreNamingLower(), 'name', '\Some\Class\Name'],
[self::underscoreNamingLower(), 'name2test', '\Some\Class\Name2Test'],
[self::underscoreNamingLower(), 'name2_test', '\Some\Class\Name2Test'],
[self::underscoreNamingLower(), 'name2test', '\Some\Class\Name2test'],
[self::underscoreNamingUpper(), 'SOME_CLASS_NAME', '\Name\Space\SomeClassName'],
[self::underscoreNamingUpper(), 'NAME', '\Some\Class\Name'],
[self::underscoreNamingUpper(), 'NAME2TEST', '\Some\Class\Name2Test'],

// NumberAwareUnderscoreNamingStrategy
[self::numberAwareUnderscoreNamingLower(), 'some_class_name', '\Name\Space\SomeClassName'],
[self::numberAwareUnderscoreNamingLower(), 'name', '\Some\Class\Name'],
[self::numberAwareUnderscoreNamingLower(), 'name2_test', '\Some\Class\Name2Test'],
[self::numberAwareUnderscoreNamingLower(), 'name2test', '\Some\Class\Name2test'],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_CLASS_NAME', '\Name\Space\SomeClassName'],
[self::numberAwareUnderscoreNamingUpper(), 'NAME', '\Some\Class\Name'],
[self::numberAwareUnderscoreNamingUpper(), 'NAME2_TEST', '\Some\Class\Name2Test'],
[self::numberAwareUnderscoreNamingUpper(), 'NAME2TEST', '\Some\Class\Name2test'],
[self::underscoreNamingUpper(), 'NAME2_TEST', '\Some\Class\Name2Test'],
[self::underscoreNamingUpper(), 'NAME2TEST', '\Some\Class\Name2test'],
];
}

Expand Down Expand Up @@ -102,23 +84,13 @@ public static function dataPropertyToColumnName(): array

// UnderscoreNamingStrategy
[self::underscoreNamingLower(), 'some_property', 'someProperty'],
[self::underscoreNamingLower(), 'base64encoded', 'base64Encoded'],
[self::underscoreNamingLower(), 'base64_encoded', 'base64Encoded'],
[self::underscoreNamingLower(), 'base64encoded', 'base64encoded'],
[self::underscoreNamingUpper(), 'SOME_PROPERTY', 'someProperty'],
[self::underscoreNamingUpper(), 'SOME_PROPERTY', 'some_property'],
[self::underscoreNamingUpper(), 'SOME_PROPERTY', 'SOME_PROPERTY'],
[self::underscoreNamingUpper(), 'BASE64ENCODED', 'base64Encoded'],
[self::underscoreNamingUpper(), 'BASE64_ENCODED', 'base64Encoded'],
[self::underscoreNamingUpper(), 'BASE64ENCODED', 'base64encoded'],

// NumberAwareUnderscoreNamingStrategy
[self::numberAwareUnderscoreNamingLower(), 'some_property', 'someProperty'],
[self::numberAwareUnderscoreNamingLower(), 'base64_encoded', 'base64Encoded'],
[self::numberAwareUnderscoreNamingLower(), 'base64encoded', 'base64encoded'],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_PROPERTY', 'someProperty'],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_PROPERTY', 'some_property'],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_PROPERTY', 'SOME_PROPERTY'],
[self::numberAwareUnderscoreNamingUpper(), 'BASE64_ENCODED', 'base64Encoded'],
[self::numberAwareUnderscoreNamingUpper(), 'BASE64ENCODED', 'base64encoded'],
];
}

Expand All @@ -144,10 +116,6 @@ public static function dataReferenceColumnName(): array
// UnderscoreNamingStrategy
[self::underscoreNamingLower(), 'id'],
[self::underscoreNamingUpper(), 'ID'],

// NumberAwareUnderscoreNamingStrategy
[self::numberAwareUnderscoreNamingLower(), 'id'],
[self::numberAwareUnderscoreNamingUpper(), 'ID'],
];
}

Expand Down Expand Up @@ -175,17 +143,11 @@ public static function dataJoinColumnName(): array

// UnderscoreNamingStrategy
[self::underscoreNamingLower(), 'some_column_id', 'someColumn', null],
[self::underscoreNamingLower(), 'base64encoded_id', 'base64Encoded', null],
[self::underscoreNamingLower(), 'base64_encoded_id', 'base64Encoded', null],
[self::underscoreNamingLower(), 'base64encoded_id', 'base64encoded', null],
[self::underscoreNamingUpper(), 'SOME_COLUMN_ID', 'someColumn', null],
[self::underscoreNamingUpper(), 'BASE64ENCODED_ID', 'base64Encoded', null],

// NumberAwareUnderscoreNamingStrategy
[self::numberAwareUnderscoreNamingLower(), 'some_column_id', 'someColumn', null],
[self::numberAwareUnderscoreNamingLower(), 'base64_encoded_id', 'base64Encoded', null],
[self::numberAwareUnderscoreNamingLower(), 'base64encoded_id', 'base64encoded', null],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_COLUMN_ID', 'someColumn', null],
[self::numberAwareUnderscoreNamingUpper(), 'BASE64_ENCODED_ID', 'base64Encoded', null],
[self::numberAwareUnderscoreNamingUpper(), 'BASE64ENCODED_ID', 'base64encoded', null],
[self::underscoreNamingUpper(), 'BASE64_ENCODED_ID', 'base64Encoded', null],
[self::underscoreNamingUpper(), 'BASE64ENCODED_ID', 'base64encoded', null],

// JoinColumnClassNamingStrategy
[new JoinColumnClassNamingStrategy(), 'classname_someColumn_id', 'someColumn', 'Some\ClassName'],
Expand Down Expand Up @@ -220,23 +182,13 @@ public static function dataJoinTableName(): array

// UnderscoreNamingStrategy
[self::underscoreNamingLower(), 'some_class_name_class_name', 'SomeClassName', 'Some\ClassName', null],
[self::underscoreNamingLower(), 'class1test_class2test', 'Class1Test', 'Some\Class2Test', null],
[self::underscoreNamingLower(), 'class1_test_class2_test', 'Class1Test', 'Some\Class2Test', null],
[self::underscoreNamingLower(), 'some_class_name_class_name', '\SomeClassName', 'ClassName', null],
[self::underscoreNamingLower(), 'name_class_name', '\Some\Class\Name', 'ClassName', null],
[self::underscoreNamingUpper(), 'SOME_CLASS_NAME_CLASS_NAME', 'SomeClassName', 'Some\ClassName', null],
[self::underscoreNamingUpper(), 'CLASS1TEST_CLASS2TEST', 'Class1Test', 'Some\Class2Test', null],
[self::underscoreNamingUpper(), 'CLASS1_TEST_CLASS2_TEST', 'Class1Test', 'Some\Class2Test', null],
[self::underscoreNamingUpper(), 'SOME_CLASS_NAME_CLASS_NAME', '\SomeClassName', 'ClassName', null],
[self::underscoreNamingUpper(), 'NAME_CLASS_NAME', '\Some\Class\Name', 'ClassName', null],

// NumberAwareUnderscoreNamingStrategy
[self::numberAwareUnderscoreNamingLower(), 'some_class_name_class_name', 'SomeClassName', 'Some\ClassName', null],
[self::numberAwareUnderscoreNamingLower(), 'class1_test_class2_test', 'Class1Test', 'Some\Class2Test', null],
[self::numberAwareUnderscoreNamingLower(), 'some_class_name_class_name', '\SomeClassName', 'ClassName', null],
[self::numberAwareUnderscoreNamingLower(), 'name_class_name', '\Some\Class\Name', 'ClassName', null],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_CLASS_NAME_CLASS_NAME', 'SomeClassName', 'Some\ClassName', null],
[self::numberAwareUnderscoreNamingUpper(), 'CLASS1_TEST_CLASS2_TEST', 'Class1Test', 'Some\Class2Test', null],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_CLASS_NAME_CLASS_NAME', '\SomeClassName', 'ClassName', null],
[self::numberAwareUnderscoreNamingUpper(), 'NAME_CLASS_NAME', '\Some\Class\Name', 'ClassName', null],
];
}

Expand Down Expand Up @@ -266,24 +218,14 @@ public static function dataJoinKeyColumnName(): array
[self::defaultNaming(), 'name_identifier', '\Some\Class\Name', 'identifier', null],

// UnderscoreNamingStrategy
[self::underscoreNamingLower(), 'some_class_name2test_id', 'SomeClassName2Test', null, null],
[self::underscoreNamingLower(), 'some_class_name2_test_id', 'SomeClassName2Test', null, null],
[self::underscoreNamingLower(), 'some_class_name_id', 'SomeClassName', null, null],
[self::underscoreNamingLower(), 'class_name_identifier', '\Some\Class\ClassName', 'identifier', null],
[self::underscoreNamingLower(), 'name2test_identifier', '\Some\Class\Name2Test', 'identifier', null],
[self::underscoreNamingLower(), 'name2_test_identifier', '\Some\Class\Name2Test', 'identifier', null],
[self::underscoreNamingUpper(), 'SOME_CLASS_NAME_ID', 'SomeClassName', null, null],
[self::underscoreNamingUpper(), 'SOME_CLASS_NAME2TEST_ID', 'SomeClassName2Test', null, null],
[self::underscoreNamingUpper(), 'SOME_CLASS_NAME2_TEST_ID', 'SomeClassName2Test', null, null],
[self::underscoreNamingUpper(), 'CLASS_NAME_IDENTIFIER', '\Some\Class\ClassName', 'IDENTIFIER', null],
[self::underscoreNamingUpper(), 'NAME2TEST_IDENTIFIER', '\Some\Class\Name2Test', 'IDENTIFIER', null],

// NumberAwareUnderscoreNamingStrategy
[self::numberAwareUnderscoreNamingLower(), 'some_class_name2_test_id', 'SomeClassName2Test', null, null],
[self::numberAwareUnderscoreNamingLower(), 'some_class_name_id', 'SomeClassName', null, null],
[self::numberAwareUnderscoreNamingLower(), 'class_name_identifier', '\Some\Class\ClassName', 'identifier', null],
[self::numberAwareUnderscoreNamingLower(), 'name2_test_identifier', '\Some\Class\Name2Test', 'identifier', null],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_CLASS_NAME_ID', 'SomeClassName', null, null],
[self::numberAwareUnderscoreNamingUpper(), 'SOME_CLASS_NAME2_TEST_ID', 'SomeClassName2Test', null, null],
[self::numberAwareUnderscoreNamingUpper(), 'CLASS_NAME_IDENTIFIER', '\Some\Class\ClassName', 'IDENTIFIER', null],
[self::numberAwareUnderscoreNamingUpper(), 'NAME2_TEST_IDENTIFIER', '\Some\Class\Name2Test', 'IDENTIFIER', null],
[self::underscoreNamingUpper(), 'NAME2_TEST_IDENTIFIER', '\Some\Class\Name2Test', 'IDENTIFIER', null],
];
}

Expand Down
35 changes: 0 additions & 35 deletions tests/Doctrine/Tests/ORM/Mapping/UnderscoreNamingStrategyTest.php

This file was deleted.

0 comments on commit 6034473

Please sign in to comment.