@@ -28,18 +28,29 @@ class RemoveUnusedAttributesCommand extends Command
2828 */
2929 private $ searchCriteriaBuilderFactory ;
3030
31+ /**
32+ * Constructor
33+ *
34+ * @param ResourceConnection $resourceConnection
35+ * @param AttributeRepositoryInterface $attributeRepository
36+ * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
37+ * @param string|null $name
38+ */
3139 public function __construct (
3240 ResourceConnection $ resourceConnection ,
3341 AttributeRepositoryInterface $ attributeRepository ,
3442 SearchCriteriaBuilderFactory $ searchCriteriaBuilderFactory ,
3543 string $ name = null
3644 ) {
3745 parent ::__construct ($ name );
38- $ this ->resourceConnection = $ resourceConnection ;
39- $ this ->attributeRepository = $ attributeRepository ;
46+ $ this ->resourceConnection = $ resourceConnection ;
47+ $ this ->attributeRepository = $ attributeRepository ;
4048 $ this ->searchCriteriaBuilderFactory = $ searchCriteriaBuilderFactory ;
4149 }
4250
51+ /**
52+ * @inheritdoc
53+ */
4354 protected function configure ()
4455 {
4556 $ this
@@ -49,76 +60,95 @@ protected function configure()
4960 ->addOption ('force ' );
5061 }
5162
63+ /**
64+ * @inheritdoc
65+ */
5266 public function execute (InputInterface $ input , OutputInterface $ output ): int
5367 {
5468 $ isDryRun = $ input ->getOption ('dry-run ' );
5569 $ isForce = $ input ->getOption ('force ' );
5670
5771 if (!$ isDryRun && !$ isForce ) {
5872 if (!$ input ->isInteractive ()) {
59- $ output ->writeln ('ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively. ' );
60-
61- return 1 ; // error.
73+ $ output ->writeln (
74+ '<error> '
75+ //phpcs:ignore Generic.Files.LineLength.TooLong
76+ . 'ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively. '
77+ . '</error> '
78+ );
79+
80+ return Command::FAILURE ;
6281 }
6382
64- $ output ->writeln ('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run. ' );
83+ $ output ->writeln (
84+ '<info>WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.</info> '
85+ );
6586 $ question = new ConfirmationQuestion ('Are you sure you want to continue? [No] ' , false );
6687
6788 if (!$ this ->getHelper ('question ' )->ask ($ input , $ output , $ question )) {
68- return 1 ; // error.
89+ return Command:: FAILURE ;
6990 }
7091 }
7192
72- $ db = $ this ->resourceConnection ->getConnection ('core_write ' );
73- $ deleted = 0 ;
74- $ searchCriteria = $ this ->searchCriteriaBuilderFactory ->create ()
93+ $ db = $ this ->resourceConnection ->getConnection ('core_write ' );
94+
95+ $ searchCriteria = $ this ->searchCriteriaBuilderFactory ->create ()
7596 ->addFilter ('is_user_defined ' , 1 )
7697 ->addFilter ('backend_type ' , 'static ' , 'neq ' )
7798 ->create ();
78- $ attributes = $ this ->attributeRepository
99+ $ attributes = $ this ->attributeRepository
79100 ->getList (ProductAttributeInterface::ENTITY_TYPE_CODE , $ searchCriteria )
80101 ->getItems ();
81- $ eavAttributeTable = $ this ->resourceConnection ->getTableName ('eav_attribute ' );
102+ $ eavAttributeTable = $ this ->resourceConnection ->getTableName ('eav_attribute ' );
82103 $ eavEntityAttributeTable = $ this ->resourceConnection ->getTableName ('eav_entity_attribute ' );
83104
105+ $ deleted = 0 ;
84106 foreach ($ attributes as $ attribute ) {
85- $ table = $ this ->resourceConnection ->getTableName ('catalog_product_entity_ ' . $ attribute ->getBackendType ());
107+ $ table = $ this ->resourceConnection
108+ ->getTableName (sprintf ('catalog_product_entity_%s ' , $ attribute ->getBackendType ()));
86109 /* Look for attributes that have no values set in products */
87- $ attributeValues = (int )$ db ->fetchOne ('SELECT COUNT(*) FROM ' . $ table . ' WHERE attribute_id = ? ' ,
88- [$ attribute ->getAttributeId ()]);
110+ $ select = $ db ->select ()
111+ ->from ($ table , ['COUNT(*) ' ])
112+ ->where ('attribute_id = ? ' , $ attribute ->getAttributeId ());
113+ $ attributeValues = (int )$ db ->fetchOne ($ select );
89114
90115 if ($ attributeValues === 0 ) {
91- $ output ->writeln ($ attribute ->getAttributeCode () . ' has ' . $ attributeValues
92- . ' values; deleting attribute ' );
116+ $ output ->writeln (
117+ sprintf ('%s has %d values; deleting attribute ' , $ attribute ->getAttributeCode (), $ attributeValues )
118+ );
93119
94120 if (!$ isDryRun ) {
95- $ db ->query ('DELETE FROM ' . $ eavAttributeTable . ' WHERE attribute_code = ? ' ,
96- $ attribute ->getAttributeCode ());
121+ $ db ->delete ($ eavAttributeTable , ['attribute_code = ? ' => $ attribute ->getAttributeCode ()]);
97122 }
98123
99124 $ deleted ++;
100125 } else {
101126 /* Look for attributes that are not assigned to attribute sets */
102- $ attributeGroups = (int )$ db ->fetchOne ('SELECT COUNT(*) FROM ' . $ eavEntityAttributeTable
103- . ' WHERE attribute_id = ? ' , [$ attribute ->getAttributeId ()]);
127+ $ select = $ db ->select ()
128+ ->from ($ eavEntityAttributeTable , ['COUNT(*) ' ])
129+ ->where ('attribute_id = ? ' , $ attribute ->getAttributeId ());
130+ $ attributeGroups = (int )$ db ->fetchOne ($ select );
104131
105132 if ($ attributeGroups === 0 ) {
106- $ output ->writeln ($ attribute ->getAttributeCode ()
107- . ' is not assigned to any attribute set; deleting attribute and its ' . $ attributeValues
108- . ' orphaned value(s) ' );
133+ $ output ->writeln (
134+ sprintf (
135+ '%s is not assigned to any attribute set; deleting attribute and its %d orphaned value(s) ' ,
136+ $ attribute ->getAttributeCode (),
137+ $ attributeValues
138+ )
139+ );
109140
110141 if (!$ isDryRun ) {
111- $ db ->query ('DELETE FROM ' . $ eavAttributeTable . ' WHERE attribute_code = ? ' ,
112- $ attribute ->getAttributeCode ());
142+ $ db ->delete ($ eavAttributeTable , ['attribute_code = ? ' => $ attribute ->getAttributeCode ()]);
113143 }
114144
115145 $ deleted ++;
116146 }
117147 }
118148 }
119149
120- $ output ->writeln ('Deleted ' . $ deleted . ' attributes. ' );
150+ $ output ->writeln (sprintf ( 'Deleted %d attributes. ' , $ deleted) );
121151
122- return 0 ; // success.
152+ return Command:: SUCCESS ;
123153 }
124154}
0 commit comments